Skip to content

Commit

Permalink
docs: add 1219
Browse files Browse the repository at this point in the history
  • Loading branch information
Aden-Q committed May 14, 2024
1 parent b5b9d55 commit 71beab5
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/1219.Path-with-Maximum-Gold.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
maxScore = 0

def backtrack(row, col, curScore):
nonlocal maxScore
if row < 0 or row >= m or col < 0 or col >= n:
return

if grid[row][col] == 0:
return

oldScore = grid[row][col]
curScore += oldScore
maxScore = max(maxScore, curScore)
grid[row][col] = 0

backtrack(row+1, col, curScore)
backtrack(row-1, col, curScore)
backtrack(row, col+1, curScore)
backtrack(row, col-1, curScore)

# backtrack
grid[row][col] = oldScore

for row in range(m):
for col in range(n):
backtrack(row, col, 0)

return maxScore

0 comments on commit 71beab5

Please sign in to comment.