This repository has been archived by the owner on Jul 30, 2020. It is now read-only.
forked from alqamahjsr/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 5
/
1219_Path_with_Maximum_Gold.py
34 lines (29 loc) · 1.71 KB
/
1219_Path_with_Maximum_Gold.py
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
class Solution(object):
def getMaximumGold(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
totalGoldCollections = []
for rowIdx in range(len(grid)):
for colIdx in range(len(grid[0])):
if grid[rowIdx][colIdx] != 0:
self.calculateGoldCollections(rowIdx, colIdx, 0, grid, totalGoldCollections)
return max(totalGoldCollections)
def calculateGoldCollections(self, rowIdx, colIdx, currentCollection, grid, totalGoldCollections):
if rowIdx < 0 or rowIdx >= len(grid) or colIdx < 0 or colIdx >= len(grid[0]) or grid[rowIdx][colIdx] == 0:
totalGoldCollections.append(currentCollection)
return # Backtrack
currentCellGold = grid[rowIdx][colIdx]
grid[rowIdx][colIdx] = 0 # To prevent revisiting the cell
currentCollection += currentCellGold
self.calculateGoldCollections(rowIdx - 1, colIdx, currentCollection, grid,
totalGoldCollections) # walk one step to the up
self.calculateGoldCollections(rowIdx, colIdx - 1, currentCollection, grid,
totalGoldCollections) # walk one step to the left
self.calculateGoldCollections(rowIdx + 1, colIdx, currentCollection, grid,
totalGoldCollections) # walk one step to the down
self.calculateGoldCollections(rowIdx, colIdx + 1, currentCollection, grid,
totalGoldCollections) # walk one step to the right
grid[rowIdx][colIdx] = currentCellGold # Restoring the grid
currentCollection -= currentCellGold # recalculating collections