The Skyline Problem

Compute the skyline of given rectangular buildings in a 2D city, eliminating hidden lines and forming the outer contour of the silhouette when viewed from a distance. Each building is described by its geometric information in the form of an array BUILDINGS[i] = [LEFT_i, RIGHT_i, HEIGHT_i].

Explanation:

  • LEFT_i: x-coordinate of the left edge of the i-th building.
  • RIGHT_i: x-coordinate of the right edge of the i-th building.
  • HEIGHT_i: height of the i-th building.

It is assumed that all buildings are perfect rectangles grounded at height 0.

The skyline must be represented as a list of "key points" sorted by x-coordinate in the format [[x1, y1], [x2, y2], ...]. Each key point marks the left endpoint of a horizontal segment in the skyline, except for the last point, which always has y-coordinate 0 to indicate the skyline's termination.

Input:

The first line contains an integer N indicating the number of buildings.
Each of the following N lines contains three space-separated integers LEFT_i, RIGHT_i, and HEIGHT_i for each building.

Output:

Return a list of key points representing the skyline.
The format is [[x1, y1], [x2, y2], ...], with each point marking a key horizontal segment.

Example:

Input:
N = 2
BUILDINGS = [[2, 9, 10], [3, 7, 15]]

Output:
[[2, 10], [3, 15], [7, 10], [9, 0]]

Constraints:

  • 1 <= |BUILDINGS| <= 10^4
  • 0 <= LEFT_i < RIGHT_i <= 2^31 - 1
  • 1 <= HEIGHT_i <= 2^31 - 1
  • Time Limit: 1 sec

Note:

Ensure no consecutive horizontal segments of equal height in the output. If any, merge them into one.
Be the first one to answer
Add answer anonymously...
Standard Chartered Full Stack Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter