Ctrl K

The Only Hilltop on the Map

Medium
A terrain map is given as an m × n matrix heights, where each cell holds its height. Cells that share a side never have equal heights. Everything beyond the edge of the map is lower than any cell. A **hilltop** is a cell strictly higher than each of its neighbours above, below, to the left and to the right. The map is guaranteed to have exactly one hilltop. Return its coordinates [row, column] (0-based). Scanning every cell in O(m · n) is easy; try to find the hilltop in O(m log n) or O(n log m).
Example 1
heights=[[1, 2, 3], [8, 9, 4], [7, 6, 5]] → [1, 1]

Cell (1, 1) with height 9 is higher than all four neighbours. There is no other hilltop: every other cell has a higher neighbour.

Example 2
heights=[[4, 6, 10, 7]] → [0, 2]

The map is a single row. Anything beyond the edge counts as lower than every cell, so the hilltop is 10 in column 2.

Example 3
heights=[[2, 3], [5, 4], [8, 6]] → [2, 0]
Constraints
  • 1 ≤ m, n ≤ 500
  • 1 ≤ heights[i][j] ≤ 10⁵
  • Cells sharing a side have different heights
  • The map has exactly one hilltop
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFind in a Row-Chained Grid MediumFind in a Grid Sorted Both Ways MediumK-th Smallest in a Sorted Grid MediumRoute With the Highest Bottleneck MediumA Value Shared by Every Row Medium
loading Python…
Input
heights=[[1, 2, 3], [8, 9, 4], [7, 6, 5]]
Expected
[1, 1]

Run checks the first 3 tests in your browser. Submit checks all 44 tests on the server, hidden ones included.