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

Leetcode Problem 218. The Skyline Problem

218. The Skyline Problem

Leetcode Solutions

Sweep Line + Priority Queue

  1. Create a list of all building edges (both left and right) along with their heights (positive for left, negative for right).
  2. Sort the edges by their x-coordinate.
  3. Initialize a priority queue (max-heap) to keep track of the current building heights intersecting the sweep line.
  4. Initialize a variable to keep track of the previous maximum height (initialized to 0).
  5. Iterate over the sorted edges: a. If the edge is a left edge, add its height to the priority queue. b. If the edge is a right edge, remove the corresponding height from the priority queue. c. After adding/removing the height, check the current maximum height in the priority queue. d. If the current maximum height is different from the previous maximum height, add a key point to the result. e. Update the previous maximum height.
  6. Return the list of key points as the skyline.
UML Thumbnail

Divide-and-Conquer

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...