Skip to content

Commit 102751c

Browse files
HARD/src/hard/TheSkylineProblem.java
1 parent fc9658c commit 102751c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

HARD/src/hard/TheSkylineProblem.java

+19
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@
44
import java.util.Arrays;
55
import java.util.List;
66
import java.util.TreeMap;
7+
/**This video is super clear and helpful: https://www.youtube.com/watch?v=GSBLe8cKu0s
8+
9+
Algorithm:
10+
First observation: all the points in the final result come from the four angles that each building has
11+
Scan through the horizontal lines
12+
Use a PriorityQueue to hold each building, and make the PriorityQueue to sort on the height of the buildings
13+
whenever we encounter the start of a building, we push it into the PriorityQueue, whenever we finished scanning that building, we remove it from the PriorityQueue
14+
Also, in the scan process, we’ll keep updating the maxHeight in the PriorityQueue if we find a new maxHeight which means the building will be overshadowed by the new higher one
15+
16+
17+
Three edge cases (see the graph illustration in the above video at 12’18”):
18+
when two buildings have the same start point, the one with higher height shows up in the final result
19+
when two buildings have the same end point, the one with higher height shows up in the final result
20+
when the start point of one building is is also the end point of another building, the one with higher height shows up in the final result
21+
22+
23+
We use TreeMap over a normal PriorityQueue:
24+
For the sake of efficiency (better time complexity), we’ll use TreeMap which supports O(logn) for remove() operation, this is the reason we choose TreeMap over a normal PriorityQueue in Java (PriorityQueue supports add() and getMaxVal() in both O(logn) time, however, for remove(), it does NOT.)
25+
But TreeMap in Java supports all the three operations in O(logn) time.*/
726

827
public class TheSkylineProblem {
928

0 commit comments

Comments
 (0)