/329. Longest Increasing Path in a Matrix

329. Longest Increasing Path in a Matrix

Hard
Arrays56.4% acceptance

Given a 2D integer grid grid of dimensions m x n, return the length of the longest strictly increasing path in the grid. You can move from each cell to its adjacent cells in four directions: up, down, left, or right. Diagonal movement and wrap-around are not allowed. Each move must go to a cell with a strictly greater value than the current cell.

Example 1

Input: [[5,3,6],[7,8,2],[1,4,9]]

Output: 5

Explanation: One possible longest path is [3,4,6,7,8].

Example 2

Input: [[10,11],[9,12]]

Output: 4

Explanation: One possible longest path is [9,10,11,12].

Example 3

Input: [[42]]

Output: 1

Explanation: Only one cell, so path length is 1.

Constraints

  • 1 <= len(grid) <= 200
  • 1 <= len(grid[0]) <= 200
  • 0 <= grid[i][j] <= 231 - 1 for all valid i, j
Python (current runtime)

Case 1

Input: [[1,2,3],[6,5,4],[7,8,9]]

Expected: 9

Case 2

Input: [[2,2],[2,2]]

Expected: 1

Case 3

Input: [[1,3,2],[6,5,4],[7,8,9]]

Expected: 7

Case 4

Input: [[100,99,98],[97,96,95],[94,93,92]]

Expected: 1

Case 5

Input: [[1,10,2],[9,3,8],[4,7,5]]

Expected: 4