diff --git a/1219. Path with Maximum Gold b/1219. Path with Maximum Gold new file mode 100644 index 0000000..3a71593 --- /dev/null +++ b/1219. Path with Maximum Gold @@ -0,0 +1,33 @@ +class Solution { +public: + vector> next = {{0,1},{0,-1},{1,0},{-1,0}}; + int getMaximumGold(vector>& g) { + int res =0,n=g.size(),m=g[0].size(); + for(int i=0;i>& g, int r, int c, int n, int m) { + if(!isValid(r, c, n, m) || g[r][c]==0) return 0; + int currVal = g[r][c]; + g[r][c]=0; + int res = currVal; + int nextRes = 0; + for(int i=0;i<4;i++) { + int nextR = r + next[i][0]; + int nextC = c + next[i][1]; + nextRes = max(backTrack(g,nextR, nextC,n,m), nextRes); + } + g[r][c]=currVal; + return res + nextRes; + } + + bool isValid(int r, int c, int n, int m) { + bool res = (r>=0 && c>=0 && r