File tree Expand file tree Collapse file tree 1 file changed +7
-10
lines changed
src/main/kotlin/g0201_0300/s0218_the_skyline_problem Expand file tree Collapse file tree 1 file changed +7
-10
lines changed Original file line number Diff line number Diff line change 11package g0201_0300.s0218_the_skyline_problem
22
33// #Hard #Array #Heap_Priority_Queue #Ordered_Set #Divide_and_Conquer #Segment_Tree
4- // #Binary_Indexed_Tree #Line_Sweep #2022_10_25_Time_365_ms_(93.14%)_Space_45.7_MB_(93.71 %)
4+ // #Binary_Indexed_Tree #Line_Sweep #2025_03_29_Time_50_ms_(100.00%)_Space_63.12_MB_(10.53 %)
55
66import java.util.TreeMap
77
@@ -12,21 +12,18 @@ class Solution {
1212 return ans
1313 }
1414 val totalBuildings = blds.size
15- val buildings = Array < Building ?> (totalBuildings * 2 ) { null }
16- var idx = 0
17- for (building in blds) {
18- buildings[idx] = Building (building[ 0 ], building[ 2 ], true )
19- buildings[idx + 1 ] = Building (building[ 1 ], building [2 ], false )
20- idx + = 2
15+ val buildings = Array (totalBuildings * 2 ) { i ->
16+ if (i % 2 == 0 ) {
17+ Building (blds[i / 2 ][ 0 ], blds[i / 2 ][ 2 ], true )
18+ } else {
19+ Building (blds[i / 2 ][ 1 ], blds[i / 2 ] [2 ], false )
20+ }
2121 }
2222 buildings.sort()
2323 val skyline = TreeMap <Int , Int >()
2424 skyline[0 ] = 1
2525 var prevMaxHeight = 0
2626 for (building in buildings) {
27- if (building == null ) {
28- continue
29- }
3027 val height = building.height
3128 if (building.isStart) {
3229 skyline[height] = 1 + (skyline[height] ? : 0 )
You can’t perform that action at this time.
0 commit comments