108. Longest Increasing Path in a Matrix

You are given an m x n integer matrix. Your task is to find the length of the longest strictly increasing path within the matrix.

From each cell, you can move in four directions: up, down, left, or right. You cannot move diagonally, and you cannot move outside the boundaries of the matrix (i.e., no wrap-around).

Example 1:

Input: matrix = [[9, 9, 4], [6, 6, 8], [2, 1, 1]]

Output: 4

Explanation:

The longest increasing path is [1, 2, 6, 9].

Example 2:

Input: matrix = [[3, 4, 5], [3, 2, 6], [2, 2, 1]]

Output: 4

Explanation: The longest increasing path is [3, 4, 5, 6]. Moving diagonally is not allowed.

Now Your Turn!

Pick the correct output for the given input

Input: matrix = [[1, 2], [4, 3]]

(What is the length of the longest increasing path?)

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 200
  • 0 <= matrix[i][j] <= 231 - 1

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int longestIncreasingPath(vector<vector<int>>& matrix) {
}
};
Test Case

Input:

Matrix