Find a Peak Element II
Pattern: Binary search on 2D matrices
Binary search over columns. In the middle column take the biggest value. If a side neighbour is bigger, a peak lies on that side; otherwise this cell is a peak.
The problem
You are given a matrix where no two neighbouring cells are equal. A peak is a cell strictly bigger than its neighbours
above, below, left and right. The outside of the matrix counts as -1. Return the position [row, col] of any peak, in
O(R log C) or O(C log R) time. For example, [[1, 4], [3, 2]] gives [0, 1] or [1, 0].
The function name findPeakGrid matches LeetCode. Use the link above for the exact statement and limits. Since any peak
is correct, this guide's tests check that the returned cell is a peak, not that it is one exact cell.
Think first
- In one column, which cell can never lose to its neighbours above or below?
- If that cell's right neighbour is bigger, why must a peak exist to the right?
- How is this Find Peak Element with columns instead of single values?
Hints
Brute force, and why it fails
Check every cell against its four neighbours, or return the position of the largest value: O(R × C). Binary search over columns: O(R log C).
The key insight
The column maximum handles up and down for free. That leaves a one-dimensional choice, left or right, which is the same slope argument as Find Peak Element.
Dry run
| lo | 0 |
|---|---|
| hi | 2 |
mat has rows 10 20 15, 21 30 14 and 7 16 32. Columns 0 to 2 are possible.
The plan
lo = 0, hi = C - 1
while lo <= hi:
mid = lo + (hi - lo) / 2
row = index of the biggest value in column mid
left = mat[row][mid - 1] or -1, right = mat[row][mid + 1] or -1
if mat[row][mid] > left and mat[row][mid] > right: return [row, mid]
if left > mat[row][mid]: hi = mid - 1
else: lo = mid + 1
Complexity
- Time
O(R log C)- Space
O(1)
About log₂ C columns are looked at, each scanned in O(R).
Edge cases
- One cell: it is a peak.
- One row: this is Find Peak Element.
- One column: its biggest value is the peak.
I've built my logic, show the code
Java
class Solution {
public int[] findPeakGrid(int[][] mat) {
int lo = 0, hi = mat[0].length - 1; // binary search over columns
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
int row = 0; // the largest value in column mid
for (int r = 1; r < mat.length; r++) if (mat[r][mid] > mat[row][mid]) row = r;
int left = mid > 0 ? mat[row][mid - 1] : -1;
int right = mid + 1 < mat[0].length ? mat[row][mid + 1] : -1;
if (mat[row][mid] > left && mat[row][mid] > right) return new int[]{row, mid};
if (left > mat[row][mid]) hi = mid - 1; // climb towards the bigger neighbour
else lo = mid + 1;
}
return new int[]{-1, -1}; // not reached: a peak always exists
}
}C++
class Solution {
public:
vector<int> findPeakGrid(vector<vector<int>>& mat) {
int cols = static_cast<int>(mat[0].size());
int lo = 0, hi = cols - 1; // binary search over columns
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
int row = 0; // the largest value in column mid
for (size_t r = 1; r < mat.size(); r++) if (mat[r][mid] > mat[row][mid]) row = static_cast<int>(r);
int left = mid > 0 ? mat[row][mid - 1] : -1;
int right = mid + 1 < cols ? mat[row][mid + 1] : -1;
if (mat[row][mid] > left && mat[row][mid] > right) return {row, mid};
if (left > mat[row][mid]) hi = mid - 1; // climb towards the bigger neighbour
else lo = mid + 1;
}
return {-1, -1}; // not reached: a peak always exists
}
};Python
class Solution:
def findPeakGrid(self, mat: list[list[int]]) -> list[int]:
lo, hi = 0, len(mat[0]) - 1 # binary search over columns
while lo <= hi:
mid = (lo + hi) // 2
row = max(range(len(mat)), key=lambda r: mat[r][mid]) # the largest value in column mid
left = mat[row][mid - 1] if mid > 0 else -1
right = mat[row][mid + 1] if mid + 1 < len(mat[0]) else -1
if mat[row][mid] > left and mat[row][mid] > right:
return [row, mid]
if left > mat[row][mid]:
hi = mid - 1 # climb towards the bigger neighbour
else:
lo = mid + 1
return [-1, -1] # not reached: a peak always existsCommon mistakes
- Taking any cell of the middle column instead of its biggest, which breaks the up/down guarantee.
- Comparing with neighbours outside the matrix without the -1 rule.
- Expecting the global maximum. Any peak is fine.