Skip to content

Commit eb5f0ae

Browse files
committed
1219. Path with Maximum Gold
1 parent 210b901 commit eb5f0ae

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

path-with-maximum-gold.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Runtime: 56 ms
2+
// Memory Usage: 8.7 MB
3+
class Solution {
4+
public:
5+
6+
int dfs(vector<vector<int> >&grid, int i, int j) {
7+
if (i < 0 || j < 0 || i >= grid.size() || j >= grid[0].size() || grid[i][j] == 0) {
8+
return 0;
9+
}
10+
11+
int temp = grid[i][j];
12+
grid[i][j] = 0;
13+
int res = 0;
14+
res = max(res, dfs(grid, i + 1, j));
15+
res = max(res, dfs(grid, i - 1, j));
16+
res = max(res, dfs(grid, i, j + 1));
17+
res = max(res, dfs(grid, i, j - 1));
18+
grid[i][j] = temp;
19+
return res + temp;
20+
}
21+
22+
int getMaximumGold(vector<vector<int>>& grid) {
23+
int res = 0;
24+
for (int i = 0; i < grid.size(); i++) {
25+
for (int j = 0; j < grid[0].size(); j++) {
26+
res = max(res, dfs(grid, i, j));
27+
}
28+
}
29+
return res;
30+
}
31+
};

0 commit comments

Comments
 (0)