Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 61ec285

Browse files
feat: add solutions to lc problem: No.1840 (doocs#3814)
No.1840.Maximum Building Height
1 parent 7e4cfbf commit 61ec285

File tree

4 files changed

+144
-8
lines changed

4 files changed

+144
-8
lines changed

‎solution/1800-1899/1840.Maximum Building Height/README.md‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,10 @@ public:
160160
int maxBuilding(int n, vector<vector<int>>& restrictions) {
161161
auto&& r = restrictions;
162162
r.push_back({1, 0});
163-
sort(r.begin(), r.end());
164-
if (r[r.size() - 1][0] != n) r.push_back({n, n - 1});
163+
ranges::sort(r);
164+
if (r[r.size() - 1][0] != n) {
165+
r.push_back({n, n - 1});
166+
}
165167
int m = r.size();
166168
for (int i = 1; i < m; ++i) {
167169
r[i][1] = min(r[i][1], r[i - 1][1] + r[i][0] - r[i - 1][0]);
@@ -204,6 +206,47 @@ func maxBuilding(n int, restrictions [][]int) (ans int) {
204206
}
205207
```
206208

209+
#### TypeScript
210+
211+
```ts
212+
function maxBuilding(n: number, restrictions: number[][]): number {
213+
restrictions.push([1, 0]);
214+
restrictions.sort((a, b) => a[0] - b[0]);
215+
if (restrictions[restrictions.length - 1][0] !== n) {
216+
restrictions.push([n, n - 1]);
217+
}
218+
219+
const m = restrictions.length;
220+
for (let i = 1; i < m; ++i) {
221+
restrictions[i][1] = Math.min(
222+
restrictions[i][1],
223+
restrictions[i - 1][1] + restrictions[i][0] - restrictions[i - 1][0],
224+
);
225+
}
226+
227+
for (let i = m - 2; i >= 0; --i) {
228+
restrictions[i][1] = Math.min(
229+
restrictions[i][1],
230+
restrictions[i + 1][1] + restrictions[i + 1][0] - restrictions[i][0],
231+
);
232+
}
233+
234+
let ans = 0;
235+
for (let i = 0; i < m - 1; ++i) {
236+
const t = Math.floor(
237+
(restrictions[i][1] +
238+
restrictions[i + 1][1] +
239+
restrictions[i + 1][0] -
240+
restrictions[i][0]) /
241+
2,
242+
);
243+
ans = Math.max(ans, t);
244+
}
245+
246+
return ans;
247+
}
248+
```
249+
207250
<!-- tabs:end -->
208251

209252
<!-- solution:end -->

‎solution/1800-1899/1840.Maximum Building Height/README_EN.md‎

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,19 @@ We can build the buildings with heights [0,1,2,3,3,4,4,5,4,3], and the tallest b
8080

8181
<!-- solution:start -->
8282

83-
### Solution 1
83+
### Solution 1: Sorting + Mathematics
84+
85+
First, we sort all the constraints by the building number in ascending order.
86+
87+
Then we traverse all the constraints from left to right. For each constraint, we can get an upper bound on the maximum height, i.e., $r_i[1] = \min(r_i[1], r_{i-1}[1] + r_i[0] - r_{i-1}[0]),ドル where $r_i$ represents the $i$-th constraint, and $r_i[0]$ and $r_i[1]$ represent the building number and the upper bound on the maximum height of the building, respectively.
88+
89+
Next, we traverse all the constraints from right to left. For each constraint, we can get an upper bound on the maximum height, i.e., $r_i[1] = \min(r_i[1], r_{i+1}[1] + r_{i+1}[0] - r_i[0])$.
90+
91+
In this way, we obtain the upper bound on the maximum height for each constrained building.
92+
93+
The problem asks for the height of the tallest building. We can enumerate the buildings between two adjacent constraints $i$ and $i+1$. To maximize the height, the height should first increase and then decrease. Suppose the maximum height is $t,ドル then $t - r_i[1] + t - r_{i+1}[1] \leq r_{i+1}[0] - r_i[0],ドル i.e., $t \leq \frac{r_i[1] + r_{i+1}[1] + r_{i+1}[0] - r_{i}[0]}{2}$. We take the maximum value of all such $t$.
94+
95+
The time complexity is $O(m \times \log m),ドル and the space complexity is $O(m)$. Here, $m$ is the number of constraints.
8496

8597
<!-- tabs:start -->
8698

@@ -146,8 +158,10 @@ public:
146158
int maxBuilding(int n, vector<vector<int>>& restrictions) {
147159
auto&& r = restrictions;
148160
r.push_back({1, 0});
149-
sort(r.begin(), r.end());
150-
if (r[r.size() - 1][0] != n) r.push_back({n, n - 1});
161+
ranges::sort(r);
162+
if (r[r.size() - 1][0] != n) {
163+
r.push_back({n, n - 1});
164+
}
151165
int m = r.size();
152166
for (int i = 1; i < m; ++i) {
153167
r[i][1] = min(r[i][1], r[i - 1][1] + r[i][0] - r[i - 1][0]);
@@ -190,6 +204,47 @@ func maxBuilding(n int, restrictions [][]int) (ans int) {
190204
}
191205
```
192206

207+
#### TypeScript
208+
209+
```ts
210+
function maxBuilding(n: number, restrictions: number[][]): number {
211+
restrictions.push([1, 0]);
212+
restrictions.sort((a, b) => a[0] - b[0]);
213+
if (restrictions[restrictions.length - 1][0] !== n) {
214+
restrictions.push([n, n - 1]);
215+
}
216+
217+
const m = restrictions.length;
218+
for (let i = 1; i < m; ++i) {
219+
restrictions[i][1] = Math.min(
220+
restrictions[i][1],
221+
restrictions[i - 1][1] + restrictions[i][0] - restrictions[i - 1][0],
222+
);
223+
}
224+
225+
for (let i = m - 2; i >= 0; --i) {
226+
restrictions[i][1] = Math.min(
227+
restrictions[i][1],
228+
restrictions[i + 1][1] + restrictions[i + 1][0] - restrictions[i][0],
229+
);
230+
}
231+
232+
let ans = 0;
233+
for (let i = 0; i < m - 1; ++i) {
234+
const t = Math.floor(
235+
(restrictions[i][1] +
236+
restrictions[i + 1][1] +
237+
restrictions[i + 1][0] -
238+
restrictions[i][0]) /
239+
2,
240+
);
241+
ans = Math.max(ans, t);
242+
}
243+
244+
return ans;
245+
}
246+
```
247+
193248
<!-- tabs:end -->
194249

195250
<!-- solution:end -->

‎solution/1800-1899/1840.Maximum Building Height/Solution.cpp‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ class Solution {
33
int maxBuilding(int n, vector<vector<int>>& restrictions) {
44
auto&& r = restrictions;
55
r.push_back({1, 0});
6-
sort(r.begin(), r.end());
7-
if (r[r.size() - 1][0] != n) r.push_back({n, n - 1});
6+
ranges::sort(r);
7+
if (r[r.size() - 1][0] != n) {
8+
r.push_back({n, n - 1});
9+
}
810
int m = r.size();
911
for (int i = 1; i < m; ++i) {
1012
r[i][1] = min(r[i][1], r[i - 1][1] + r[i][0] - r[i - 1][0]);
@@ -19,4 +21,4 @@ class Solution {
1921
}
2022
return ans;
2123
}
22-
};
24+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function maxBuilding(n: number, restrictions: number[][]): number {
2+
restrictions.push([1, 0]);
3+
restrictions.sort((a, b) => a[0] - b[0]);
4+
if (restrictions[restrictions.length - 1][0] !== n) {
5+
restrictions.push([n, n - 1]);
6+
}
7+
8+
const m = restrictions.length;
9+
for (let i = 1; i < m; ++i) {
10+
restrictions[i][1] = Math.min(
11+
restrictions[i][1],
12+
restrictions[i - 1][1] + restrictions[i][0] - restrictions[i - 1][0],
13+
);
14+
}
15+
16+
for (let i = m - 2; i >= 0; --i) {
17+
restrictions[i][1] = Math.min(
18+
restrictions[i][1],
19+
restrictions[i + 1][1] + restrictions[i + 1][0] - restrictions[i][0],
20+
);
21+
}
22+
23+
let ans = 0;
24+
for (let i = 0; i < m - 1; ++i) {
25+
const t = Math.floor(
26+
(restrictions[i][1] +
27+
restrictions[i + 1][1] +
28+
restrictions[i + 1][0] -
29+
restrictions[i][0]) /
30+
2,
31+
);
32+
ans = Math.max(ans, t);
33+
}
34+
35+
return ans;
36+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /