FastPrepCount Starts Reaching a Grid Maximum

Count Starts Reaching a Grid Maximum

Duolingo logoDuolingo● MediumINTERNPHONE SCREEN
Learn

Problem statement

You are given a rectangular height matrix. From a cell, inspect all eight neighboring cells and move to the strictly higher neighbor with greatest height. If several eligible neighbors have that height, choose the one with smaller row, then smaller column. Stop when no higher neighbor exists.

Return the number of starting cells whose path ends at any cell whose value equals the global maximum of the matrix.

Function

countStartsReachingMaximum(heights: int[][]) → int

Examples

Example 1

heights = [[1,2],[3,4]]return = 4

Every cell moves directly or indirectly to the global maximum 4.

Example 2

heights = [[9,1,8],[1,7,1],[6,1,5]]return = 7

Seven starts reach 9; the cell 8 and the adjacent 1 that chooses it stop at the non-global local maximum.

Constraints

  • 1 <= heights.length, heights[0].length
  • heights.length * heights[0].length <= 200000
  • -1000000000 <= heights[r][c] <= 1000000000
  • All rows have equal length.

More Duolingo problems

See Duolingo hiring insights
public int countStartsReachingMaximum(int[][] heights) {
    // Write your code here.
}
heights[[1,2],[3,4]]
expected4
Checking account…