From 5454b9ca8eb0301d1c283e70ef86ac19f2856f63 Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Tue, 14 May 2024 17:30:16 +0530 Subject: [PATCH] Create 1219. Path with Maximum Gold --- 1219. Path with Maximum Gold | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 1219. Path with Maximum Gold 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