Loading...
You are given a two-dimensional list of integers grid with m rows and n columns.
A path starts at any cell and repeatedly moves to a neighboring cell that holds a strictly larger value. The neighbors of a cell are the cells directly above, below, to the left, and to the right of it. A path can't move diagonally and can't leave the grid. A path can also stop right away, so a single cell on its own is a path.
Return the number of cells in the longest path.
Input: grid = [[9,9,4],[6,6,8],[2,1,1]]
Output: 4
Explanation: Start at the 1 in the bottom row, move left to 2, up to 6, then up to 9. Each move goes to a neighbor with a larger value, so the path has 4 cells. It can't continue to the other 9 because 9 isn't strictly larger than 9. No path has 5 cells.
Input: grid = [[3,4,5],[3,2,6],[2,2,1]]
Output: 4
Explanation: Start at the 3 in the top-left corner, move right to 4, right to 5, then down to 6. That is 4 cells. The 2 in the middle can't reach the 5 because diagonal moves aren't allowed.
Input: grid = [[1]]
Output: 1
Explanation: The only path is the single cell.
m = grid.lengthn = grid[i].lengthm ≤200n ≤200grid[i][j] ≤231−1