-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm2684.py
28 lines (23 loc) · 1.04 KB
/
m2684.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
class Solution:
def maxMoves(self, grid: List[List[int]]) -> int:
def dfs(grid: List[List[int]], r: int, c: int) -> int :
# last column reached
if c == len(grid[0]) - 1 :
return 0
# check for -inf already visited marker
if grid[r][c] < 0 :
return 0
# if any of the next potential squares is valid
output = 0
if grid[r][c + 1] > grid[r][c] :
output = max(output, 1 + dfs(grid, r, c + 1))
if r < len(grid) - 1 and grid[r + 1][c + 1] > grid[r][c] :
output = max(output, 1 + dfs(grid, r + 1, c + 1))
if r > 0 and grid[r - 1][c + 1] > grid[r][c] :
output = max(output, 1 + dfs(grid, r - 1, c + 1))
# mark current square as visited since any other that reaches
# this square will have the same score
grid[r][c] = -inf
# return
return output
return max([dfs(grid, r, 0) for r in range(len(grid))])