File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Time: O(nlogn)
2
+ // Space: O(1)
3
+
4
+ class Solution {
5
+ public:
6
+ int maxBuilding (int n, vector<vector<int >>& restrictions) {
7
+ restrictions.push_back ({1 , 0 });
8
+ restrictions.push_back ({n, n - 1 });
9
+ sort (begin (restrictions), end (restrictions));
10
+ for (int i = size (restrictions) - 2 ; i >= 0 ; --i) {
11
+ restrictions[i][1 ] = min (restrictions[i][1 ], restrictions[i + 1 ][1 ] + (restrictions[i + 1 ][0 ] - restrictions[i][0 ]));
12
+ }
13
+ int result = 0 ;
14
+ for (int i = 1 ; i < size (restrictions); ++i) {
15
+ restrictions[i][1 ] = min (restrictions[i][1 ], restrictions[i - 1 ][1 ] + (restrictions[i][0 ] - restrictions[i - 1 ][0 ]));
16
+ int left = restrictions[i - 1 ][0 ], h1 = restrictions[i - 1 ][1 ];
17
+ int right = restrictions[i][0 ], h2 = restrictions[i][1 ];
18
+ result = max (result, max (h1, h2) + ((right - left) - abs (h1 - h2)) / 2 );
19
+ }
20
+ return result;
21
+ }
22
+ };
You can’t perform that action at this time.
0 commit comments