-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPath with Maximum Gold.cpp
More file actions
42 lines (32 loc) · 996 Bytes
/
Path with Maximum Gold.cpp
File metadata and controls
42 lines (32 loc) · 996 Bytes
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
class Solution {
public:
int help(vector<vector<int>>& grid, int i, int j)
{
if(i<0 || j<0 || i>=grid.size() || j>=grid[0].size() || grid[i][j]==0)
return 0;
int temp = grid[i][j];
grid[i][j] = 0;
int op1 = help(grid, i+1, j);
int op2 = help(grid, i-1, j);
int op3 = help(grid, i, j-1);
int op4 = help(grid, i, j+1);
grid[i][j] = temp;
return ( grid[i][j] + max({op1, op2, op3, op4}) );
}
int getMaximumGold(vector<vector<int>>& grid)
{
int call, ans = 0;
for(int i=0;i<grid.size();i++)
{
for(int j=0;j<grid[0].size();j++)
{
if(grid[i][j] != 0)
{
call = help(grid,i,j);
ans = max(call ,ans);
}
}
}
return ans;
}
};