Finding the Number of Visible Mountains - Problem
You are given a 0-indexed 2D integer array peaks where peaks[i] = [xi, yi] states that mountain i has a peak at coordinates (xi, yi).
A mountain can be described as a right-angled isosceles triangle, with its base along the x-axis and a right angle at its peak. More formally, the gradients of ascending and descending the mountain are 1 and -1 respectively.
A mountain is considered visible if its peak does not lie within another mountain (including the border of other mountains).
Return the number of visible mountains.
Input & Output
Example 1 — Basic Visibility
$
Input:
peaks = [[2,1],[2,2],[1,4]]
›
Output:
2
💡 Note:
Mountain at (2,1) has base [1,3], mountain at (2,2) has base [0,4], mountain at (1,4) has base [-3,5]. The first mountain is completely contained within the second (same left boundary but smaller right), so only 2 mountains are visible.
Example 2 — All Visible
$
Input:
peaks = [[1,3],[1,1],[2,2]]
›
Output:
3
💡 Note:
Mountain bases are [-2,4], [0,2], [0,4]. No mountain completely contains another, so all 3 are visible.
Example 3 — Duplicate Peaks
$
Input:
peaks = [[0,1],[1,0]]
›
Output:
1
💡 Note:
Both peaks (0,1) and (1,0) create the same base range [-1,1] and [1,1]. Since they represent the same mountain shape, only 1 is counted as visible.
Constraints
- 1 ≤ peaks.length ≤ 105
- peaks[i].length == 2
- 1 ≤ xi, yi ≤ 105
Visualization
Tap to expand
Understanding the Visualization
1
Input Peaks
Mountain peaks at coordinates (x,y)
2
Base Conversion
Convert to base intervals [x-y, x+y]
3
Visibility Check
Count mountains not contained by others
Key Takeaway
🎯 Key Insight: Transform 2D mountain peaks into 1D interval containment problem using base ranges
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code