forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPath with Maximum Gold.py
55 lines (47 loc) · 1.68 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Runtime: 1480 ms (Top 97.21%) | Memory: 14.1 MB (Top 13.08%)
class Solution:
def getMaximumGold(self, grid):
answer = [0]
def visit(visited, i, j, gold_sum):
val = grid[i][j]
if val == 0 or (i,j) in visited:
answer[0] = max(answer[0], gold_sum)
return
gold_sum_new = gold_sum + val
visited_new = visited.union({(i,j)})
if i > 0:
visit(visited_new, i-1, j, gold_sum_new)
if j < len(grid[i]) - 1:
visit(visited_new, i, j+1, gold_sum_new)
if i < len(grid) - 1:
visit(visited_new, i+1, j, gold_sum_new)
if j > 0:
visit(visited_new, i, j-1, gold_sum_new)
#choosing the starting points
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] != 0:
count = 0
try:
if grid[i-1][j] != 0:
count += 1
except:
pass
try:
if grid[i][j+1] != 0:
count += 1
except:
pass
try:
if grid[i+1][j] != 0:
count += 1
except:
pass
try:
if grid[i][j-1] != 0:
count += 1
except:
pass
if count < 3:
visit(set(),i,j,0)
return answer[0]