Advertisement

The Skyline Problem - LeetCode 218 Solution

The Skyline Problem - Complete Solution Guide

The Skyline Problem is LeetCode problem 218, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Given the locations and heights of all the buildings, return the skyline formed by these buildings collectively . The geometric information of each building is given in the array buildings where buildings[i] = [left i , right i , height i ] : left i is the x coordinate of the left edge of the i th building. right i is the x coordinate of the right edge of the i th buildin

Detailed Explanation

The Skyline Problem asks us to determine the outer contour of a city's skyline given the locations and heights of its buildings. The input is a list of buildings, where each building is represented by a tuple (left, right, height), indicating the x-coordinates of its left and right edges and its height. The output should be a list of key points, representing the skyline. These key points are the left endpoints of the horizontal segments that make up the skyline's contour. The skyline should be sorted by x-coordinate, and there should be no consecutive horizontal lines of equal height. The last point always has a y-coordinate of 0 to mark the end of the skyline.

Solution Approach

The provided solution uses a line sweep algorithm along with a max-heap to efficiently determine the skyline. It processes the buildings from left to right based on their start and end x-coordinates. Each building contributes two events: a start event (left edge, negative height) and an end event (right edge, positive height). The negative height is used to distinguish start events from end events when sorting. The max-heap stores the heights of all active buildings at a given x-coordinate. By maintaining this max-heap, we can determine the current maximum height and identify points where the skyline changes.

Step-by-Step Algorithm

  1. Step 1: Create a list of points, where each point represents either the start or end of a building. Store these points as tuples (x-coordinate, height), using negative heights to represent start events and positive heights to represent end events.
  2. Step 2: Sort the points by their x-coordinate. If two points have the same x-coordinate, prioritize start events (negative height) over end events (positive height) or events with larger heights.
  3. Step 3: Initialize a max-heap to keep track of the current active building heights. Initially, the max-heap contains a single element, 0, representing the ground level.
  4. Step 4: Initialize a map (or dictionary) to keep track of heights to be deleted because heights are not easily removed from a max-heap
  5. Step 5: Iterate through the sorted points. For each point:
  6. Step 5a: If it's a start event (negative height), add the height to the max-heap.
  7. Step 5b: If it's an end event (positive height), mark the height for deletion.
  8. Step 6: After processing each x-coordinate, remove heights from the heap that were marked for deletion
  9. Step 7: Get the current maximum height from the max-heap.
  10. Step 8: Compare the current maximum height with the height of the last key point in the skyline. If they are different, add a new key point (x-coordinate, current maximum height) to the skyline. Handle cases where the new key point has the same x-coordinate as the previous one (update the height) or has the same height as the previous one (remove the redundant key point).
  11. Step 9: Return the skyline.

Key Insights

  • Insight 1: Treat the start and end of each building as separate events. The start event increases the current maximum height, and the end event decreases it. These events drive the skyline's changes.
  • Insight 2: Use a data structure (max-heap) to efficiently track the current maximum height at any given x-coordinate. This allows us to quickly determine if a skyline point needs to be added or updated.
  • Insight 3: Handle overlapping and adjacent buildings correctly. The solution should gracefully handle cases where buildings share the same x-coordinate or have varying heights that influence the skyline's shape.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(n)

Topics

This problem involves: Array, Divide and Conquer, Binary Indexed Tree, Segment Tree, Line Sweep, Sorting, Heap (Priority Queue), Ordered Set.

Companies

Asked at: Citadel, Goldman Sachs, Google, Salesforce, Siemens, X, Yelp.