[LeetCode] Find the Maximum Number of Fruits Collected

3363. Find the Maximum Number of Fruits Collected

There is a game dungeon comprised of n x n rooms arranged in a grid.

You are given a 2D array fruits of size n x n, where fruits[i][j] represents the number of fruits in the room (i, j). Three children will play in the game dungeon, with initial positions at the corner rooms (0, 0), (0, n - 1), and (n - 1, 0).

The children will make exactly n - 1 moves according to the following rules to reach the room (n - 1, n - 1):

  • The child starting from (0, 0) must move from their current room (i, j) to one of the rooms (i + 1, j + 1), (i + 1, j), and (i, j + 1) if the target room exists.
  • The child starting from (0, n - 1) must move from their current room (i, j) to one of the rooms (i + 1, j - 1), (i + 1, j), and (i + 1, j + 1) if the target room exists.
  • The child starting from (n - 1, 0) must move from their current room (i, j) to one of the rooms (i - 1, j + 1), (i, j + 1), and (i + 1, j + 1) if the target room exists.

When a child enters a room, they will collect all the fruits there. If two or more children enter the same room, only one child will collect the fruits, and the room will be emptied after they leave.

Return the maximum number of fruits the children can collect from the dungeon.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Solution {
void helper(vector<vector<int>>& A, vector<pair<int,int>> D, int y, int x) {
int n = A.size(), m = A[0].size();
vector<vector<array<int,3>>> vis(n, vector<array<int,3>>(m, {-1,-1,-1}));
queue<array<int,3>> q;
auto push = [&](int cy, int cx, int cc, int y, int x) {
if(vis[cy][cx][0] < cc) {
vis[cy][cx] = {cc,y,x};
q.push({cy,cx,cc});
}
};
q.push({y,x,0}); vis[y][x][0] = 0;
auto reachAble = [&](int cy, int cx) {
if(y == 0 and x == 0) return true;
if(x == n - 1) {
int dis = n - 1 - cy;
int dix = m - 1 - cx;
return dix <= dis;
}
if(y == m - 1) {
int dis = n - 1 - cy;
int dix = m - 1 - cx;
return dis <= dix;
}
return false;
};
while(q.size()) {
auto [y,x,cost] = q.front(); q.pop();
if(vis[y][x][0] != cost) continue;
for(auto& [dy,dx] : D) {
int ny = y + dy, nx = x + dx;
if(0 <= ny and ny < n and 0 <= nx and nx < m and reachAble(ny,nx)) {
push(ny,nx,cost + A[ny][nx], y, x);
}
}
}

y = x = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(vis[y][x][0] < vis[i][j][0]) y = i, x = j;
}
}
while(y >= 0 and x >= 0) {
A[y][x] = 0;
int ny = vis[y][x][1], nx = vis[y][x][2];
y = ny, x = nx;
}

}
public:
int maxCollectedFruits(vector<vector<int>>& fruits) {
int res = 0, n = fruits.size();
for(auto& vec : fruits) for(auto& x : vec) res += x;

helper(fruits, {{1,1}}, 0,0);

helper(fruits, {{1,-1},{1,0},{1,1}}, 0, n - 1);
helper(fruits, {{-1,1},{0,1},{1,1}}, n - 1,0);


for(auto& vec : fruits) for(auto& x : vec) res -= x;
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/11/24/PS/LeetCode/find-the-maximum-number-of-fruits-collected/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.