File tree 1 file changed +43
-0
lines changed
1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[][] } buildings
3
+ * @return {number[][] }
4
+ */
5
+ var getSkyline = function ( buildings ) {
6
+ const edgeSet = new Set ( ) ;
7
+ for ( let i = 0 ; i < buildings . length ; i ++ ) {
8
+ const [ from , to ] = buildings [ i ] ;
9
+ edgeSet . add ( from ) ;
10
+ edgeSet . add ( to ) ;
11
+ }
12
+ const positions = [ ...edgeSet ] ;
13
+ positions . sort ( ( a , b ) => a - b ) ;
14
+
15
+ const pq = new PriorityQueue ( { compare : ( a , b ) => b [ 2 ] - a [ 2 ] } ) ;
16
+
17
+ const result = [ ] ;
18
+
19
+ let j = 0 ;
20
+ for ( let i = 0 ; i < positions . length ; i ++ ) {
21
+ const position = positions [ i ] ;
22
+
23
+ for ( j ; j < buildings . length && buildings [ j ] [ 0 ] <= position ; j ++ ) {
24
+ pq . enqueue ( buildings [ j ] ) ;
25
+ }
26
+
27
+ while ( ! pq . isEmpty ( ) && pq . front ( ) [ 1 ] <= position ) {
28
+ pq . dequeue ( ) ;
29
+ }
30
+
31
+ let maxHeight = pq . front ( ) ?. [ 2 ] ?? 0 ;
32
+
33
+ if ( ! result . length || result . at ( - 1 ) [ 1 ] !== maxHeight ) {
34
+ result . push ( [ position , maxHeight ] ) ;
35
+ }
36
+ }
37
+
38
+ return result ;
39
+ } ;
40
+
41
+ // another
42
+
43
+
1
44
/**
2
45
* @param {number[][] } buildings
3
46
* @return {number[][] }
You can’t perform that action at this time.
0 commit comments