LeetCode 329 Longest Increasing Path in a Matrix - Solution

The core idea

DFS with memoization. Let dp[i][j] = longest increasing path starting from (i,j).

From cell (i,j), try all 4 neighbors with larger values. The longest path from (i,j) is 1 + max of paths from valid neighbors.

Since paths are strictly increasing, there are no cycles. Safe to memoize without worrying about infinite loops.