Imagine you're standing on a 2D mountain range represented as a matrix, where each cell contains an elevation value. Your goal is to find a peak element - a position that is strictly higher than all of its neighboring positions (left, right, top, and bottom).
Given an m x n matrix mat where no two adjacent cells have equal values, find any peak element and return its coordinates [i, j].
Important: The matrix is surrounded by an imaginary border of -1 values, making edge and corner elements easier to be peaks. You must solve this efficiently in O(m log n) or O(n log m) time complexity.
Example: In matrix [[1,4,3],[2,5,1]], element 5 at position [1,1] is a peak because it's greater than all neighbors (4, 3, 2, 1).
Input & Output
Visualization
Time & Space Complexity
We visit every cell once and check up to 4 neighbors for each
Only using constant extra space for variables
Constraints
- m == mat.length
- n == mat[i].length
- 1 โค m, n โค 500
- -105 โค mat[i][j] โค 105
- No two adjacent cells are equal
- You must achieve O(m log n) or O(n log m) time complexity