Longest Increasing Path in a Matrix
Longest Increasing Path in a Matrix
Problem Statement
Given an m x n
integers matrix
, return the length of the longest increasing path in matrix
.
From each cell, you can either move in four directions: left, right, up, or down. You may not move diagonally or move outside the boundary (i.e., wrap-around is not allowed).
Example
Output: 4
Explanation: The longest increasing path is [1, 2, 6, 9].
Solution
This problem can be modeled as finding the longest path in a Directed Acyclic Graph (DAG), where the cells are nodes and an edge exists from cell A to B if B is adjacent to A and has a greater value.
A brute-force DFS from every cell would be too slow due to recomputing the same paths. We can optimize this by using memoization (a form of dynamic programming) to cache the results. We'll use a cache grid to store the length of the longest increasing path starting from each cell.
Algorithm Steps (DFS + Memoization)
- Initialize a `cache` grid of the same size as the matrix to store computed path lengths.
- Iterate through each cell `(r, c)` of the matrix.
- For each cell, perform a DFS to find the longest increasing path starting from it.
- The DFS function will:
- Check the cache first. If the result for `(r, c)` is already computed, return it.
- Explore all 4 neighbors. If a neighbor is valid and has a strictly greater value, recursively call DFS on it.
- The longest path from `(r, c)` is 1 + the maximum path length from its valid neighbors.
- Store this result in `cache[r][c]` before returning.
- Keep track of the overall maximum path length found from all starting cells.
Longest Increasing Path
Max Length Found: 0