Find a Peak Element II - Problem
A peak element in a 2D grid is an element that is strictly greater than all of its adjacent neighbors to the left, right, top, and bottom.
Given a 0-indexed m x n matrix mat where no two adjacent cells are equal, find any peak element mat[i][j] and return the length 2 array [i,j].
You may assume that the entire matrix is surrounded by an outer perimeter with the value -1 in each cell.
You must write an algorithm that runs in O(m log(n)) or O(n log(m)) time.
Input & Output
Example 1 — Basic 3x3 Matrix
$
Input:
mat = [[1,4,3,2,0],[2,7,6,1,3],[5,0,1,4,8]]
›
Output:
[1,1]
💡 Note:
Element 7 at [1,1] is greater than all neighbors: 7 > 4 (top), 7 > 2 (left), 7 > 6 (right), 7 > 0 (bottom).
Example 2 — Single Peak
$
Input:
mat = [[10,20,15],[21,30,14],[7,16,32]]
›
Output:
[1,1]
💡 Note:
Element 30 at [1,1] is greater than all neighbors: 30 > 20, 30 > 21, 30 > 14, 30 > 16
Example 3 — Corner Peak
$
Input:
mat = [[1,2],[3,4]]
›
Output:
[1,1]
💡 Note:
Element 4 at [1,1] is greater than all neighbors: 4 > 3, 4 > 2. Boundary cells have value -1.
Constraints
- m == mat.length
- n == mat[i].length
- 1 ≤ m, n ≤ 500
- -105 ≤ mat[i][j] ≤ 105
- No two adjacent cells are equal
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code