Find a Peak Element II
Real-world version: Any Summit in a Height FieldThe statement here is our own: the same inputs, outputs and technique.
A height field is stored in mat, where mat[r][c] is the altitude of cell r, c. No two cells that share an edge hold the same altitude.
A cell is a summit when its altitude is strictly above the altitude of every cell sharing an edge with it. Cells on the border have fewer neighbours to beat, and everything off the edge of the field counts as lower than any cell, so a summit always exists.
Return the position of a summit as the pair [r, c]. When the field holds more than one summit, any one of them is accepted.
Aim for a routine that does not look at every cell: a running time on the order of rows * log(columns) or columns * log(rows) is expected.
Examples
Example 1
- Input
- mat = [[1, 4, 3]]
- Output
- [0, 1]
Cell 0, 1 holds 4, which is above both 1 and 3, and it is the only summit in this field.
Example 2
- Input
- mat = [[5, 8, 6], [9, 20, 4]]
- Output
- [1, 1]
Cell 1, 1 holds 20 and beats 8 above it, 9 to its left and 4 to its right, so it is a summit.
Example 3
- Input
- mat = [[10, 2, 9], [3, 1, 4], [8, 5, 7]]
- Output
- [0, 0]
Cell 0, 0 holds 10 and beats 2 to its right and 3 below it. The cells at 0, 2 and at 2, 0 and at 2, 2 are summits too, so any of those four answers is accepted.
Constraints
- m == mat.length
- n == mat[i].length
- 1 <= m, n <= 500
- 1 <= mat[i][j] <= 10^5
- No two cells sharing an edge hold equal altitudes.
More than one answer is valid. Return any one of them.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def find_peak_grid(mat: list[list[int]]) -> list[int]:public int[] findPeakGrid(int[][] mat)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 11 test cases: 11 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Count Negative Numbers in a Sorted MatrixReal-world version: Freezing Cells on the Weather BoardEasy
- Search a 2D MatrixReal-world version: Paged Offset LookupMedium
- Kth Smallest Element in a Sorted MatrixReal-world version: Kth Smallest Drift ReadingMedium
- Path With Minimum EffortReal-world version: Route With the Gentlest ClimbMedium