Ezvor

2345. Finding the Number of Visible Mountains

MediumArray Preparing judge… LeetCode
ArrayStackSortingMonotonic Stack

Hints

Hint 1
How can we efficiently find for each mountain the relevant mountains to compare itself against to check whether or not it would be visible?
Hint 2
Do you notice a pattern after sorting the peaks by their x-coordinates?
Hint 3
After sorting, process the peaks sequentially and use a monotonic stack to store currently visible mountains.