Problem Description
Given a list of mountain peaks represented as points (x, y), each mountain is modeled as a right‑angled isosceles triangle with its base on the x‑axis and the right angle at the peak. Its left and right boundaries are given by (x – y) and (x + y) respectively. A mountain is considered visible if its peak does not lie inside or exactly on the border of any other mountain’s triangle. The task is to determine the number of visible mountains.
Key Insights
- A mountain with peak (x, y) spans an interval [x – y, x + y] on the x‑axis.
- Mountain i is completely covered by mountain j if and only if (xj – yj) ≤ (xi – yi) and (xi + yi) ≤ (xj + yj). Note that equality counts as “within”.
- Sorting the mountains by their left boundary (x – y) and then by right boundary (x + y) in descending order lets us “sweep” and check if the current mountain’s right boundary is covered by any previous mountain.
- When two or more mountains have exactly the same boundaries, each peak lies on the border of the other. In such cases, none of them is visible.
Space and Time Complexity
Time Complexity: O(n log n) due to sorting. Space Complexity: O(n) for storing the transformed intervals and frequency counts.
Solution
We first transform each mountain’s peak (x, y) into an interval [L, R] where L = x – y and R = x + y. Next, we count how many mountains share the exact same (L, R) interval – if more than one mountain has the same interval, then they hide each other.
After this, we sort the unique intervals by L (ascending) and then by R (descending). We then iterate over these sorted intervals while keeping track of the maximum R encountered so far. For a mountain with frequency 1, if its R value is less than or equal to the current maximum R, then its peak is covered by an earlier mountain; otherwise, it is visible. Mountains with duplicates (frequency greater than 1) are automatically marked as hidden.
Key data structures and techniques used:
- A list of intervals with a frequency count.
- Sorting to ensure that a mountain that can cover subsequent mountains comes early.
- A greedy “sweep-line” style update of the maximum R to test coverage.
This method efficiently determines whether each mountain’s peak is “covered” by checking the boundaries.