Maximum Building Height LeetCode solution in C++

1
class Solution {
2
public:
3
int maxBuilding(int n, vector<vector<int>> &restrictions) {
4
restrictions.push_back({1, 0});
5
restrictions.push_back({n, n - 1});
6
sort(restrictions.begin(), restrictions.end());
7
for (int i = restrictions.size() - 2; i >= 0; --i) {
8
restrictions[i][1] = min(
9
restrictions[i][1], restrictions[i + 1][1] + restrictions[i + 1][0] - restrictions[i][0]);
10
}
11

12
int ans = 0;
13
for (int i = 1; i < restrictions.size(); ++i) {
14
restrictions[i][1] = min(
15
restrictions[i][1], restrictions[i - 1][1] + restrictions[i][0] - restrictions[i - 1][0]);
16
ans = max(ans, (restrictions[i - 1][1] + restrictions[i][0] - restrictions[i - 1][0] +
17
restrictions[i][1]) /
18
2);
19
}
20
return ans;
21
}
22
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0