Ctrl K

Peaks in Plain Sight

Medium
A panorama shows mountains standing on a flat horizon (the x axis). Mountain i is given by its summit peaks[i] = [x, y]: it is an isosceles right triangle with the right angle at the summit, both slopes at 45°, and its base on the x axis from x - y to x + y. A summit is visible if it is not inside another mountain and does not touch another mountain's slope. Identical mountains hide each other. Return the number of visible summits.
Example 1
peaks=[[3, 3], [9, 3], [8, 1]] → 2

The slope of the mountain (9, 3) is at height 2 at x = 8, above the peak (8, 1), so that peak is hidden. The mountains (3, 3) and (9, 3) do not hide each other. Two peaks are visible.

Example 2
peaks=[[4, 2], [4, 2], [10, 1]] → 1

The two identical mountains hide each other, so only (10, 1) is visible.

Example 3
peaks=[[2, 1], [5, 4]] → 1

The slope of (5, 4) is at height 1 at x = 2, so the peak (2, 1) lies right on it and counts as hidden.

Constraints
  • 1 ≤ len(peaks) ≤ 10⁵
  • peaks[i] = [x, y]
  • 1 ≤ x, y ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFix the Middle of the Line MediumMost Pieces Sorted Separately MediumConvoys at the Finish MediumOutclassed Heroes MediumLongest Drop From End to End Medium
loading Python…
Input
peaks=[[3, 3], [9, 3], [8, 1]]
Expected
2

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