Longest Consecutive Decreasing Grid Path
Problem statement
Given a nonempty integer matrix grid, return the maximum number of cells in a path whose values decrease consecutively.
From a cell with value x, the next cell must share an edge with it and have value exactly x - 1. A path may start at any cell and move up, down, left, or right.
Function
longestConsecutiveDecreasingPath(grid: int[][]) → intExamples
Example 1
grid = [[5,4,3],[6,1,2],[7,0,-1]]return = 9The path follows 7,6,5,4,3,2,1,0,-1.
Example 2
grid = [[3,2],[2,1]]return = 3Either cell containing 2 can connect 3 to 1.
Example 3
grid = [[4,1],[2,0]]return = 2The longest path is 1,0; diagonal cells are not adjacent.
Constraints
1 <= grid.length, grid[0].length <= 200.- Every row has the same length.
- Every value is a signed 32-bit integer.