1
+ // Runtime: 143 ms (Top 19.44%) | Memory: 114.3 MB (Top 55.56%)
1
2
class Solution {
2
3
public int maxBuilding (int n , int [][] restrictions ) {
3
4
List <int []> list =new ArrayList <>();
@@ -6,35 +7,35 @@ public int maxBuilding(int n, int[][] restrictions) {
6
7
list .add (restriction );
7
8
}
8
9
Collections .sort (list ,new IDSorter ());
9
-
10
+
10
11
if (list .get (list .size ()-1 )[0 ]!=n ){
11
12
list .add (new int []{n ,n -1 });
12
13
}
13
-
14
+
14
15
for (int i =1 ;i <list .size ();i ++){
15
16
list .get (i )[1 ]=Math .min (list .get (i )[1 ],list .get (i -1 )[1 ] + list .get (i )[0 ]-list .get (i -1 )[0 ]);
16
17
}
17
-
18
+
18
19
for (int i =list .size ()-2 ;i >=0 ;i --){
19
20
list .get (i )[1 ]=Math .min (list .get (i )[1 ],list .get (i +1 )[1 ] + list .get (i +1 )[0 ] - list .get (i )[0 ]);
20
- }
21
-
21
+ }
22
+
22
23
int result =0 ;
23
24
for (int i =1 ;i <list .size ();i ++){
24
- int h1 =list .get (i -1 )[1 ]; // heigth of previous restriction
25
- int h2 =list .get (i )[1 ]; // height of current restriction
25
+ int h1 =list .get (i -1 )[1 ]; // heigth of previous restriction
26
+ int h2 =list .get (i )[1 ]; // height of current restriction
26
27
int x =list .get (i -1 )[0 ]; // id of previous restriction
27
- int y =list .get (i )[0 ]; // id of current restriction
28
-
28
+ int y =list .get (i )[0 ]; // id of current restriction
29
+
29
30
result =Math .max (result ,Math .max (h1 ,h2 ) + (y -x -Math .abs (h1 -h2 ))/2 );
30
- }
31
+ }
31
32
return result ;
32
33
}
33
-
34
+
34
35
public class IDSorter implements Comparator <int []>{
35
36
@ Override
36
37
public int compare (int [] myself ,int [] other ){
37
38
return myself [0 ]-other [0 ];
38
- }
39
+ }
39
40
}
40
- }
41
+ }
0 commit comments