The Skyline Problem šŸ šŸ¢

beginner
24 min

The Skyline Problem šŸ šŸ¢

Welcome to a fascinating journey through one of the most intriguing problems in computer science – The Skyline Problem! This problem is a great way to dive deep into data structures and algorithms, and we'll learn it step-by-step, just like building a skyscraper. šŸ—ļø

What is The Skyline Problem? šŸ”

Imagine you're an architect designing a city skyline. Your task is to find all the unique "visible" buildings' edges from different viewpoints. The challenge lies in sorting and merging the buildings' heights and positions to get the final skyline. Sounds interesting, right? Let's dive in!

Prerequisites šŸ“

Before we start, you should be familiar with the following concepts:

  • Arrays and lists
  • Basic sorting algorithms (Bubble Sort, Selection Sort)
  • Data structures (List, Dictionary)

The Problem Statement šŸ“„

Given a list of buildings, each building i is described by an array [Li, Ri, Hi], where:

  • Li and Ri represent the x-coordinates of the left and right edge of the i-th building.
  • Hi represents the height of the i-th building.

We are now ready to find all unique "visible" points in the skyline. For each viewpoint (x, y), we search for the highest building with left edge less than or equal to x and right edge greater than x. The y-coordinate of the point is the maximum height among all such buildings.

Approach šŸ“

We'll tackle the problem in three steps:

  1. Sort the buildings by their left x-coordinates (non-decreasing order)
  2. Merge the buildings using two pointers
  3. Process the merged buildings and find the visible points

Let's break down each step and see how it works.

Step 1: Sort the Buildings šŸ“

First, we need to sort all buildings by their left x-coordinates in non-decreasing order. This step is crucial as it makes finding the highest building for each x-coordinate easier during the merging process.

Here's a simple way to sort the buildings using Selection Sort:

python
def selection_sort(buildings): for i in range(len(buildings)): min_index = i for j in range(i+1, len(buildings)): if buildings[j][0] < buildings[min_index][0]: min_index = j buildings[i], buildings[min_index] = buildings[min_index], buildings[i]

šŸ’” Pro Tip: Selection Sort is not the most efficient sorting algorithm, but it's easy to understand and implement. As you progress in your learning, you'll encounter more efficient algorithms like Merge Sort and Quick Sort.

Step 2: Merge the Buildings šŸ“

Once the buildings are sorted, we can start merging them using two pointers. Initially, we maintain two pointers: left and right, which point to the current buildings with left edges less than or equal to the current x-coordinate and greater than the current x-coordinate, respectively.

python
def merge_buildings(buildings): merged_buildings = [] x = buildings[0][0] left_index = 0 for right_index in range(1, len(buildings)): if buildings[right_index][0] > x: merged_buildings.append(buildings[left_index]) left_index += 1 x = max(x, buildings[right_index][1]) merged_buildings.append(buildings[left_index:]) return merged_buildings

Step 3: Process the Merged Buildings šŸ“

Now that we have the merged buildings, we need to process them to find the visible points. We'll maintain a list of visible points and update it whenever we find a new point or a height change for the current point.

python
def find_skyline(buildings): merged_buildings = merge_buildings(buildings) visible_points = [] for buildings_part in merged_buildings: if len(buildings_part) > 0: current_height = buildings_part[0][2] current_x = buildings_part[0][0] visible_points.append((current_x, current_height)) for building in buildings_part[1:]: if building[2] > current_height: visible_points.append((building[0], building[2])) current_height = building[2] elif building[0] < current_x and building[2] < current_height: pass # No need to update if the new building is not visible else: current_height = building[2] return visible_points

Quiz šŸŽÆ

Quick Quiz
Question 1 of 1

Given the following buildings:

Wrapping Up āœ…

Congratulations! You've now learned how to solve The Skyline Problem using data structures and algorithms. As you practice more, you'll find more efficient ways to solve this problem, but for now, you have a solid foundation to build upon.

šŸ“ Note: Keep exploring, keep coding, and keep learning! Happy coding! šŸ‘©ā€šŸ’»šŸš€