Leetcode Problem 2345. Finding the Number of Visible Mountains

2345. Finding the Number of Visible Mountains

Leetcode Solutions

Sorting and Monotonic Stack Approach

Algorithm

  1. Sort the peaks by their x-coordinates.
  2. Initialize an empty stack to keep track of visible peaks.
  3. 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.
  4. The size of the stack represents the number of visible mountains.
  5. Return the size of the stack.
UML Thumbnail

Sorting by Base Points and Linear Sweep

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...