ntree.ai
ntree.aiLongest Increasing Path in a Matrix

Longest Increasing Path in a Matrix

hardsolvedattempted

Given an m x n grid of integers matrix, return the length of the longest increasing path in it.

From each cell, the path can move left, right, up or down, but not diagonally and not outside the grid. Every step must go to a strictly greater value.

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.

Constraints:

  • 1 <= m, n <= 200
  • 0 <= matrix[i][j] <= 2^31 - 1
def longestIncreasingPath(matrix: list[list[int]]) -> int:
pass
 

Press Visualise to step through your code.