bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1762. Buildings With an Ocean View

1762. Buildings With an Ocean View

Leetcode Solutions

Linear Iteration with Monotonic Decrease

  1. Initialize an empty list result to store the indices of buildings with an ocean view.
  2. Initialize maxHeight to -1 to keep track of the tallest building seen so far.
  3. Iterate over the array of building heights from right to left (i.e., start from the last building and move towards the first).
  4. For each building, check if its height is greater than maxHeight.
    • If it is, append the current index to result and update maxHeight to the current building's height.
  5. After the loop, reverse the result list because the indices were added in descending order.
  6. Return the reversed result list.
UML Thumbnail

Naive Approach with Rightward Checks

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...