Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit102751c

Browse files
HARD/src/hard/TheSkylineProblem.java
1 parentfc9658c commit102751c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

‎HARD/src/hard/TheSkylineProblem.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@
44
importjava.util.Arrays;
55
importjava.util.List;
66
importjava.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
publicclassTheSkylineProblem {
928

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp