Skip to content

Commit ee96ec4

Browse files
authored
Create maximum-building-height.cpp
1 parent c2cee35 commit ee96ec4

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

C++/maximum-building-height.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
};

0 commit comments

Comments
 (0)