Tutorialspoint
Problem
Solution
Submissions

Skyline Problem

Certification: Advanced Level Accuracy: 0% Submissions: 0 Points: 15

Write a C++ program to solve the Skyline Problem. You are given a list of buildings where each building is represented by a triplet [left, right, height], which indicates the left edge position, right edge position, and height of the building. Your task is to output the skyline formed by these buildings collectively.

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: Transform each building into two events: one for the start edge (height increase) and one for the end edge (height decrease).
    • Step 2: Sort all events by position, with special handling for ties.
    • Step 3: Process events in order, keeping track of current heights using a priority queue or similar data structure.
    • Step 4: Record height changes in the skyline when the maximum height changes after processing an event.
Example 2
  • Input: buildings = [[0,2,3],[2,5,3]]
  • Output: [[0,3],[5,0]]
  • Explanation:
    • Step 1: Transform each building into two events: one for the start edge (height increase) and one for the end edge (height decrease).
    • Step 2: Sort all events by position, with special handling for ties.
    • Step 3: Process events in order, updating current heights.
    • Step 4: Since the buildings are at the same height and one starts where the other ends, there are only two critical points: the start of the first building (0,3) and the end of the second building (5,0).
Constraints
  • 1 ≤ buildings.length ≤ 10^4
  • 0 ≤ left < right ≤ 2^31 - 1
  • 1 ≤ height ≤ 2^31 - 1
  • Time Complexity: O(n log n) where n is the number of buildings
  • Space Complexity: O(n)
SetMapTech MahindraSamsung
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 priority queue to track the maximum height at each point
  • Process the buildings by sorting all critical points (both start and end points)
  • For each critical point, update the maximum height and add it to the result if the height changes
  • Use a balanced tree data structure like a multiset to efficiently find the maximum height
  • Handle edge cases like overlapping buildings

Steps to solve by this approach:

 Step 1: Create an array of "critical points" by adding both the start and end points of all buildings.
 Step 2: For start points, use negative height to distinguish them from end points.
 Step 3: Sort all critical points by their x-coordinate (and by height for tiebreaking).
 Step 4: Iterate through these critical points, maintaining a multiset of current heights.
 Step 5: For a start point, add the height to the multiset; for an end point, remove the corresponding height.
 Step 6: After processing each critical point, check if the maximum height has changed.
 Step 7: If the maximum height changed, add the critical point to the result.

Submitted Code :