Skip to content

Commit b54661e

Browse files
authored
Create 1219. Path with Maximum Gold (#478)
2 parents 93c9224 + 5454b9c commit b54661e

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

1219. Path with Maximum Gold

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> next = {{0,1},{0,-1},{1,0},{-1,0}};
4+
int getMaximumGold(vector<vector<int>>& g) {
5+
int res =0,n=g.size(),m=g[0].size();
6+
for(int i=0;i<n;i++) {
7+
for(int j=0;j<m;j++) {
8+
res = max(res, backTrack(g,i,j,n,m));
9+
}
10+
}
11+
return res;
12+
}
13+
14+
int backTrack(vector<vector<int>>& g, int r, int c, int n, int m) {
15+
if(!isValid(r, c, n, m) || g[r][c]==0) return 0;
16+
int currVal = g[r][c];
17+
g[r][c]=0;
18+
int res = currVal;
19+
int nextRes = 0;
20+
for(int i=0;i<4;i++) {
21+
int nextR = r + next[i][0];
22+
int nextC = c + next[i][1];
23+
nextRes = max(backTrack(g,nextR, nextC,n,m), nextRes);
24+
}
25+
g[r][c]=currVal;
26+
return res + nextRes;
27+
}
28+
29+
bool isValid(int r, int c, int n, int m) {
30+
bool res = (r>=0 && c>=0 && r<n && c<m);
31+
return res;
32+
}
33+
};

0 commit comments

Comments
 (0)