LeetCode 329 Longest Increasing Path in a Matrix - Example and Complexity Analysis

Walkthrough and analysis

Compute dp for each cell in the first example.

dp[2][1] = 1 (value 1, no smaller neighbors to come from). dp[2][0] = 1 + dp[1][0] = ? Need dp[1][0] first. dp[1][0] = 1 + max(dp[0][0], dp[1][1]). Continue...

Eventually: dp[2][1] starting with 1 → path through 2 → 6 → 9. Length 4.

Max over all dp values = 4.

Each cell computed once. O(mn)O(m \cdot n) time. Space: O(mn)O(m \cdot n) for memoization.