Leetcode Problem 2345. Finding the Number of Visible Mountains
2345. Finding the Number of Visible Mountains
Leetcode Solutions
Sorting and Monotonic Stack Approach
Algorithm
Sort the peaks by their x-coordinates.
Initialize an empty stack to keep track of visible peaks.
Iterate over the sorted peaks:
a. While the stack is not empty and the current peak covers the peak at the top of the stack, pop the stack.
b. If the stack is empty or the current peak is not covered by the peak at the top of the stack, push the current peak onto the stack.
The size of the stack represents the number of visible mountains.