Longest Increasing Path in a Matrix - Problem
Given an m x n matrix of integers, return the length of the longest increasing path in the matrix.
From each cell, you can move in four directions: left, right, up, or down. You cannot move diagonally or outside the boundary.
Input & Output
Example 1 — Basic 3x3 Matrix
$
Input:
matrix = [[9,9,4],[6,6,8],[2,1,1]]
›
Output:
4
💡 Note:
The longest increasing path is 1→6→8→9 with length 4. We can start from (2,1) with value 1, move to (1,0) with value 6, then to (1,2) with value 8, then to (0,0) or (0,1) with value 9.
Example 2 — Simple 2x2 Matrix
$
Input:
matrix = [[3,4],[5,2]]
›
Output:
3
💡 Note:
The longest increasing path is 2→3→4 with length 3. Start from (1,1) with value 2, move to (0,0) with value 3, then to (0,1) with value 4.
Example 3 — Single Cell
$
Input:
matrix = [[1]]
›
Output:
1
💡 Note:
Only one cell exists, so the longest path has length 1.
Constraints
- m == matrix.length
- n == matrix[i].length
- 1 ≤ m, n ≤ 200
- 0 ≤ matrix[i][j] ≤ 231 - 1
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code