2684. Maximum Number of Moves in a Grid
Tapan Rachchh
1 min read
class Solution:
def maxMoves(self, grid: List[List[int]]) -> int:
ans = 0
def traverse(a, b, prevVal):
nonlocal count, ans
if (a, b) in visited:
return
visited.add((a, b))
for (x, y) in ((-1, 1), (0, 1), (1, 1)):
newA = a + x
newB = b + y
if newA < len(grid) and newB < len(grid[0]) and newA >= 0 and newB >= 0:
nextVal = grid[newA][newB]
if nextVal > prevVal:
count += 1
if count > ans:
ans = count
traverse(a + x, b + y, nextVal)
count -= 1
for a, row in enumerate(grid):
for b, cell in enumerate(row[:1]):
count = 0
visited = set()
traverse(a, b, grid[a][b])
return ans
0
Subscribe to my newsletter
Read articles from Tapan Rachchh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by