Tutorialspoint
Problem
Solution
Submissions

Skyline Problem (Building Outline Problem)

Certification: Advanced Level Accuracy: 100% Submissions: 2 Points: 15

Write a Python function get_skyline(buildings) that solves the skyline problem. Each building is represented as [left, right, height], and the function returns the key points that define the skyline.

Example 1
  • Input: buildings = [[2,9,10],[3,7,15],[5,12,12],[15,20,10],[19,24,8]]
  • Output: [[2,10],[3,15],[7,12],[12,0],[15,10],[20,8],[24,0]]
  • Explanation:
    • Step 1: At x=2, start building height 10 → [2,10]
    • Step 2: At x=3, taller building 15 starts → [3,15]
    • Step 3: At x=7, height drops to 12 → [7,12]
    • Step 4: At x=12, height drops to 0 → [12,0]
    • Step 5: At x=15, building of height 10 starts → [15,10]
    • Step 6: At x=20, height drops to 8 → [20,8]
    • Step 7: At x=24, height drops to 0 → [24,0]
Example 2
  • Input: buildings = [[0,2,3],[2,5,3]]
  • Output: [[0,3],[5,0]]
  • Explanation:
    • Step 1: At x=0, height becomes 3 → [0,3]
    • Step 2: At x=5, all buildings end → [5,0]
Constraints
  • 1 ≤ buildings.length ≤ 104
  • 0 ≤ left < right ≤ 231 - 1
  • 1 ≤ height ≤ 231 - 1
  • Time Complexity: O(n log n), where n is the number of buildings
  • Space Complexity: O(n)
AlgorithmsMicrosofteBay
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use a sweep line algorithm combined with a priority queue (max heap)
  • Process all building edges (both left and right) in order of x-coordinate
  • For left edges, add the height to the max heap; for right edges, remove the height
  • Record a key point whenever the maximum height changes
  • Be careful with edge cases such as overlapping buildings and buildings with the same start or end point

Steps to solve by this approach:

 Step 1: Convert buildings into a list of points, marking start points with negative heights.
 Step 2: Sort all points by x-coordinate (and by height for same x-coordinate).
 Step 3: Process each point in order, keeping track of active building heights in a max heap.
 Step 4: For start points, add the height to the max heap; for end points, remove the corresponding height.
 Step 5: Record a key point whenever the maximum height changes, creating the skyline profile.

Submitted Code :