Skyline Problem
Skyline Problem
Interview Camp
Technique: Line Sweep
Level: Hard
You are given a list of buildings that are part of a skyline. For each building, you
are given the height, start and end points.
For example, if a building has [height=5, start=1, end=4],
it represents a building of height 5 from point 1 on a number line to point 4.
Given a list of such buildings that may overlap, you want to draw the skyline.
Questions to Clarify:
Q. Can we assume that we have a draw function that takes care of drawing?
A. Yes
Solution:
We use line sweep to loop through the buildings. We sort the start and end points, and loop
through them. When we loop, we keep a data structure (let’s say a map) that tells us the highest
building among the current overlapping buildings. The highest height is the one we draw.
© 2017 Interview Camp (interviewcamp.io)
Interview Camp
The above pseudocode will draw the top of the skyline, but won't draw the vertical lines.
You can modify it to draw vertical lines pretty easily (full pseudocode below).
In the above algorithm, we use a hash table to keep track of overlapping buildings. This makes
it quick to add and remove buildings. But, it is slow to find the max height. It takes O(n) to
find the max height, and the time complexity of this algorithm becomes O(n^2).
Can we improve the time to find the max height? Yes we can, using a heap. With a heap, we
can insert and delete nodes in log(N) time, and find the max in O(1) time. This reduces the
overall complexity to O(Nlog(N)).
There is one caveat though - to delete a node from the heap, we need a pointer to the node.
So, we keep a hash map with pointers to the heap nodes. This data structure ends up similar
to a linked hash map, except with a heap instead of a linked list.
More details about heaps are in the Heap section.
Alternatively, you can also use a Binary Search Tree instead of a Heap if you like.
In the interview, we will call the data structure BuildingMap and implement it as a Hashed Heap
only if the interviewer asks us to. In most cases, the interviewer will not ask you to implement it.
Pseudocode:
Turn Buildings into Points
Sort Points by location
Test Cases:
© 2017 Interview Camp (interviewcamp.io)
Interview Camp
Collections.sort(points);
/*
* Helper Code. Ask the interviewer if they want you to implement.
*/
public static class Building {
int height;
© 2017 Interview Camp (interviewcamp.io)
Interview Camp
int start;
int end;
@Override
public int compareTo(Point other) {
if (location == other.getLocation())
© 2017 Interview Camp (interviewcamp.io)
Interview Camp
return isStart ? -1 : 1;
return location > other.getLocation() ? 1 : -1;
}
}
/*
* This is the Hash Table implementation of BuildingMap.
*
* Time Complexity: O(1) for insertion, O(N) for max lookup.
*/
public static class BuildingMap {
HashMap<Integer, Integer> map;
public BuildingMap() {
map = new HashMap<>();
}
/*
* This is the Hashed Heap implementation of BuildingMap.
* The interviewer won't ask you to implement the actual Heap in addition to
* this question, because that would be too much work. You can assume that
* a Heap class is provided to you.
*
© 2017 Interview Camp (interviewcamp.io)
Interview Camp
* Note for Java Users: Java has a PriorityQueue class that is a heap, but
* we cannot use it in this problem. In PriorityQueue, you cannot give a
* node to delete in O(logN) time. It’s delete() function takes O(N) time
* because it searches for the node before deleting it.
*
* Time Complexity: O(log(N)) for insertion, O(1) for max lookup
*/
public static class BuildingMapWithHeap {
// The map stores a list of nodes because there can be multiple
// buildings with the same height.
HashMap<Integer, List<Node>> map;
Heap<Integer> heap;
public BuildingMap() {
map = new HashMap<>();
heap = new Heap<>();
}
© 2017 Interview Camp (interviewcamp.io)
Interview Camp
© 2017 Interview Camp (interviewcamp.io)