Count Starts Reaching a Grid Maximum
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[][]) → intExamples
Example 1
heights = [[1,2],[3,4]]return = 4Every cell moves directly or indirectly to the global maximum 4.
Example 2
heights = [[9,1,8],[1,7,1],[6,1,5]]return = 7Seven 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].lengthheights.length * heights[0].length <= 200000-1000000000 <= heights[r][c] <= 1000000000- All rows have equal length.