Find Any Local Maximum in a 2D Matrix
Implement findPeakGrid
A cell in a matrix is a peak if it's strictly greater than every neighbor sharing an edge with it (up, down, left, right) — treat a missing neighbor at the matrix's border as negative infinity. Given a matrix where no two edge-adjacent cells are equal, return the position
[row, col] of any peak — one is always guaranteed to exist.
Solve it in O(m log n) time by binary-searching over columns: find each candidate column's maximum, then let its left/right neighbors tell you which way to move — the same climbing idea as the 1D peak problem, one dimension up.
Example 1:
Input: mat = [[1,3,2]]
Output: [0,1]
Example 2:
Input: mat = [[1,2],[4,3]]
Output: [1,0]
Example 3:
Input: mat = [[5]]
Output: [0,0]
+ 4 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ matrix.length, matrix[0].length ≤ 100 - ●
1 ≤ matrix[i][j] ≤ 10⁵ - ●
No two cells that share an edge have equal values - ●
If multiple peaks exist, returning the position of any one of them is accepted
mat =
[[1,3,2]]