FastPrepLongest Consecutive Decreasing Grid Path

Longest Consecutive Decreasing Grid Path

Google logoGoogle● MediumNEW GRADPHONE SCREEN
Learn

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[][]) → int

Examples

Example 1

grid = [[5,4,3],[6,1,2],[7,0,-1]]return = 9

The path follows 7,6,5,4,3,2,1,0,-1.

Example 2

grid = [[3,2],[2,1]]return = 3

Either cell containing 2 can connect 3 to 1.

Example 3

grid = [[4,1],[2,0]]return = 2

The 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.

More Google problems

See Google hiring insights
public int longestConsecutiveDecreasingPath(int[][] grid) {
    // Write your code here.
}
grid[[5,4,3],[6,1,2],[7,0,-1]]
expected9
Checking account…