Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.1840 #3814

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions solution/1800-1899/1840.Maximum Building Height/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,10 @@ public:
int maxBuilding(int n, vector<vector<int>>& restrictions) {
auto&& r = restrictions;
r.push_back({1, 0});
sort(r.begin(), r.end());
if (r[r.size() - 1][0] != n) r.push_back({n, n - 1});
ranges::sort(r);
if (r[r.size() - 1][0] != n) {
r.push_back({n, n - 1});
}
int m = r.size();
for (int i = 1; i < m; ++i) {
r[i][1] = min(r[i][1], r[i - 1][1] + r[i][0] - r[i - 1][0]);
Expand Down Expand Up @@ -204,6 +206,47 @@ func maxBuilding(n int, restrictions [][]int) (ans int) {
}
```

#### TypeScript

```ts
function maxBuilding(n: number, restrictions: number[][]): number {
restrictions.push([1, 0]);
restrictions.sort((a, b) => a[0] - b[0]);
if (restrictions[restrictions.length - 1][0] !== n) {
restrictions.push([n, n - 1]);
}

const m = restrictions.length;
for (let i = 1; i < m; ++i) {
restrictions[i][1] = Math.min(
restrictions[i][1],
restrictions[i - 1][1] + restrictions[i][0] - restrictions[i - 1][0],
);
}

for (let i = m - 2; i >= 0; --i) {
restrictions[i][1] = Math.min(
restrictions[i][1],
restrictions[i + 1][1] + restrictions[i + 1][0] - restrictions[i][0],
);
}

let ans = 0;
for (let i = 0; i < m - 1; ++i) {
const t = Math.floor(
(restrictions[i][1] +
restrictions[i + 1][1] +
restrictions[i + 1][0] -
restrictions[i][0]) /
2,
);
ans = Math.max(ans, t);
}

return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
61 changes: 58 additions & 3 deletions solution/1800-1899/1840.Maximum Building Height/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<!-- solution:start -->

### Solution 1
### Solution 1: Sorting + Mathematics

First, we sort all the constraints by the building number in ascending order.

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.

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])$.

In this way, we obtain the upper bound on the maximum height for each constrained building.

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$.

The time complexity is $O(m \times \log m)$, and the space complexity is $O(m)$. Here, $m$ is the number of constraints.

<!-- tabs:start -->

Expand Down Expand Up @@ -146,8 +158,10 @@ public:
int maxBuilding(int n, vector<vector<int>>& restrictions) {
auto&& r = restrictions;
r.push_back({1, 0});
sort(r.begin(), r.end());
if (r[r.size() - 1][0] != n) r.push_back({n, n - 1});
ranges::sort(r);
if (r[r.size() - 1][0] != n) {
r.push_back({n, n - 1});
}
int m = r.size();
for (int i = 1; i < m; ++i) {
r[i][1] = min(r[i][1], r[i - 1][1] + r[i][0] - r[i - 1][0]);
Expand Down Expand Up @@ -190,6 +204,47 @@ func maxBuilding(n int, restrictions [][]int) (ans int) {
}
```

#### TypeScript

```ts
function maxBuilding(n: number, restrictions: number[][]): number {
restrictions.push([1, 0]);
restrictions.sort((a, b) => a[0] - b[0]);
if (restrictions[restrictions.length - 1][0] !== n) {
restrictions.push([n, n - 1]);
}

const m = restrictions.length;
for (let i = 1; i < m; ++i) {
restrictions[i][1] = Math.min(
restrictions[i][1],
restrictions[i - 1][1] + restrictions[i][0] - restrictions[i - 1][0],
);
}

for (let i = m - 2; i >= 0; --i) {
restrictions[i][1] = Math.min(
restrictions[i][1],
restrictions[i + 1][1] + restrictions[i + 1][0] - restrictions[i][0],
);
}

let ans = 0;
for (let i = 0; i < m - 1; ++i) {
const t = Math.floor(
(restrictions[i][1] +
restrictions[i + 1][1] +
restrictions[i + 1][0] -
restrictions[i][0]) /
2,
);
ans = Math.max(ans, t);
}

return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
8 changes: 5 additions & 3 deletions solution/1800-1899/1840.Maximum Building Height/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ class Solution {
int maxBuilding(int n, vector<vector<int>>& restrictions) {
auto&& r = restrictions;
r.push_back({1, 0});
sort(r.begin(), r.end());
if (r[r.size() - 1][0] != n) r.push_back({n, n - 1});
ranges::sort(r);
if (r[r.size() - 1][0] != n) {
r.push_back({n, n - 1});
}
int m = r.size();
for (int i = 1; i < m; ++i) {
r[i][1] = min(r[i][1], r[i - 1][1] + r[i][0] - r[i - 1][0]);
Expand All @@ -19,4 +21,4 @@ class Solution {
}
return ans;
}
};
};
36 changes: 36 additions & 0 deletions solution/1800-1899/1840.Maximum Building Height/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
function maxBuilding(n: number, restrictions: number[][]): number {
restrictions.push([1, 0]);
restrictions.sort((a, b) => a[0] - b[0]);
if (restrictions[restrictions.length - 1][0] !== n) {
restrictions.push([n, n - 1]);
}

const m = restrictions.length;
for (let i = 1; i < m; ++i) {
restrictions[i][1] = Math.min(
restrictions[i][1],
restrictions[i - 1][1] + restrictions[i][0] - restrictions[i - 1][0],
);
}

for (let i = m - 2; i >= 0; --i) {
restrictions[i][1] = Math.min(
restrictions[i][1],
restrictions[i + 1][1] + restrictions[i + 1][0] - restrictions[i][0],
);
}

let ans = 0;
for (let i = 0; i < m - 1; ++i) {
const t = Math.floor(
(restrictions[i][1] +
restrictions[i + 1][1] +
restrictions[i + 1][0] -
restrictions[i][0]) /
2,
);
ans = Math.max(ans, t);
}

return ans;
}