File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number } n
3+ * @param {number[][] } restrictions
4+ * @return {number }
5+ */
6+ var maxBuilding = function ( n , restrictions ) {
7+ restrictions . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ;
8+ let prevInd = 1 ,
9+ prevH = 0 ;
10+ for ( let i = 0 ; i < restrictions . length ; i ++ ) {
11+ restrictions [ i ] [ 1 ] = Math . min (
12+ restrictions [ i ] [ 1 ] ,
13+ prevH + ( restrictions [ i ] [ 0 ] - prevInd )
14+ ) ;
15+ prevInd = restrictions [ i ] [ 0 ] ;
16+ prevH = restrictions [ i ] [ 1 ] ;
17+ }
18+
19+ for ( let i = restrictions . length - 2 ; i >= 0 ; i -- ) {
20+ restrictions [ i ] [ 1 ] = Math . min (
21+ restrictions [ i ] [ 1 ] ,
22+ restrictions [ i + 1 ] [ 1 ] + ( restrictions [ i + 1 ] [ 0 ] - restrictions [ i ] [ 0 ] )
23+ ) ;
24+ }
25+
26+ let ph = 0 ,
27+ pInd = 1 ,
28+ highest = 0 ;
29+ for ( let i = 0 ; i < restrictions . length ; i ++ ) {
30+ let ind = restrictions [ i ] [ 0 ] ;
31+ let h = restrictions [ i ] [ 1 ] ;
32+ if ( ph < h ) {
33+ h = Math . min ( h , ph + ( ind - pInd ) ) ;
34+
35+ let remains = Math . max ( 0 , ind - pInd - ( h - ph ) ) ;
36+ highest = Math . max ( highest , h + ~ ~ ( remains / 2 ) ) ;
37+ } else {
38+ let remains = ind - pInd - ( ph - h ) ;
39+ highest = Math . max ( highest , ph + ~ ~ ( remains / 2 ) ) ;
40+ }
41+ ph = h ;
42+ pInd = ind ;
43+ }
44+ highest = Math . max ( highest , ph + ( n - pInd ) ) ;
45+ return highest ;
46+ } ;
You can’t perform that action at this time.
0 commit comments