Find the longest strictly rising path in a grid. Depth-first search from each cell, and remember every answer so no cell is ever solved twice.
▼The problem
LeetCode 329 (Hard). Given an m × n integer matrix, return the length of the longest strictly increasing path, moving up, down, left or right (no diagonals, no wrapping around).
Example (LeetCode's first): [[9,9,4],[6,6,8],[2,1,1]] → 4 (1, 2, 6, 9: from the middle 1 left to the 2, up to a 6, up to a 9).
def longest_increasing_path(matrix):
m, n = len(matrix), len(matrix[0])
memo = {}
def best(r, c):
if (r, c) in memo:
return memo[(r, c)]
top = 1
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nr, nc = r + dr, c + dc
if 0 <= nr < m and 0 <= nc < n:
if matrix[nr][nc] > matrix[r][c]:
top = max(top, 1 + best(nr, nc))
memo[(r, c)] = top
return top
return max(best(r, c) for r in range(m) for c in range(n))TRY IT ON LEETCODE ▶The solution
def longest_increasing_path(matrix):
m, n = len(matrix), len(matrix[0])
memo = {}
def best(r, c):
if (r, c) in memo:
return memo[(r, c)]
top = 1
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nr, nc = r + dr, c + dc
if 0 <= nr < m and 0 <= nc < n:
if matrix[nr][nc] > matrix[r][c]:
top = max(top, 1 + best(nr, nc))
memo[(r, c)] = top
return top
return max(best(r, c) for r in range(m) for c in range(n))Transcript
Longest Increasing Path in a Matrix. In a grid of numbers, find the longest path where every step goes up, down, left or right, onto a strictly bigger number. Equal doesn't count. No diagonals, and no wrapping around the edges.
Take this grid. Start on the middle one, step left to the two, up to a six, and up to a nine. Four cells, each bigger than the last: the answer is four.
The naive way searches from every cell, trying every uphill step and remembering nothing. From the two, it climbs six, nine. From the one, it climbs two, six, nine all over again. This tiny grid takes twenty-three calls. A ten by ten grid takes over seven hundred thousand: exponential.
The key idea: remember each cell's answer. A cell's best path is one, for itself, plus the best among its bigger neighbors. Solve each cell once, write it down, and later searches just read it. No visited set is needed: a path that only climbs can never loop back.
In code, a cached search starts at one and tries four directions. For each bigger neighbor inside the grid, it keeps one plus that neighbor's answer, if that's better. It stores the result. The answer is the largest over every cell.
Now the first grid, row by row. Both nines have nothing bigger: one each. The four climbs to the eight, a dead end worth one, or to a nine, also one: so two. Each six climbs to a nine: two. The two climbs to a six: three. The middle one can reach a six or the two, both already remembered, so no new search: one plus three, four. The last one climbs to the eight: two. The best of all is four.
Each cell is solved once and checks four neighbors, so the time is order m times n. The memo holds one answer per cell: order m times n space.
Only climb up, remember every cell, take the best. That's Longest Increasing Path in a Matrix.