Skip to content

Commit b90876c

Browse files
authored
Merge pull request #1556 from 0xff-dev/1840
Add solution and test-cases for problem 1840
2 parents 37fd173 + 9fb7d2a commit b90876c

6 files changed

Lines changed: 87 additions & 24 deletions

File tree

7.91 KB
Loading
10.6 KB
Loading
15.5 KB
Loading

leetcode/1801-1900/1840.Maximum-Building-Height/README.md

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,52 @@
11
# [1840.Maximum Building Height][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You want to build `n` new buildings in a city. The new buildings will be built in a line and are labeled from `1` to `n`.
5+
6+
However, there are city restrictions on the heights of the new buildings:
7+
8+
- The height of each building must be a non-negative integer.
9+
- The height of the first building **must** be `0`.
10+
- The height difference between any two adjacent buildings **cannot exceed** `1`.
11+
12+
Additionally, there are city restrictions on the maximum height of specific buildings. These restrictions are given as a 2D integer array `restrictions` where `restrictions[i] = [idi, maxHeighti]` indicates that building `idi` must have a height **less than or equal to** `maxHeighti`.
13+
14+
It is guaranteed that each building will appear **at most once** in `restrictions`, and building `1` will **not** be in `restrictions`.
15+
16+
Return the **maximum possible height** of the **tallest** building.
717

8-
**Example 1:**
18+
**Example 1:**
19+
20+
![1](./1.png)
921

1022
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
23+
Input: n = 5, restrictions = [[2,1],[4,1]]
24+
Output: 2
25+
Explanation: The green area in the image indicates the maximum allowed height for each building.
26+
We can build the buildings with heights [0,1,2,1,2], and the tallest building has a height of 2.
1327
```
1428

15-
## 题意
16-
> ...
29+
**Example 2:**
1730

18-
## 题解
31+
![2](./2.png)
1932

20-
### 思路1
21-
> ...
22-
Maximum Building Height
23-
```go
2433
```
34+
Input: n = 6, restrictions = []
35+
Output: 5
36+
Explanation: The green area in the image indicates the maximum allowed height for each building.
37+
We can build the buildings with heights [0,1,2,3,4,5], and the tallest building has a height of 5.
38+
```
39+
40+
**Example 3:**
2541

42+
![3](./3.png)
43+
44+
```
45+
Input: n = 10, restrictions = [[5,3],[2,5],[7,4],[10,3]]
46+
Output: 5
47+
Explanation: The green area in the image indicates the maximum allowed height for each building.
48+
We can build the buildings with heights [0,1,2,3,3,4,4,5,4,3], and the tallest building has a height of 5.
49+
```
2650

2751
## 结语
2852

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,43 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(n int, restrictions [][]int) int {
6+
restrictions = append(restrictions, []int{1, 0})
7+
8+
sort.Slice(restrictions, func(i, j int) bool {
9+
return restrictions[i][0] < restrictions[j][0]
10+
})
11+
12+
m := len(restrictions)
13+
14+
for i := 1; i < m; i++ {
15+
dist := restrictions[i][0] - restrictions[i-1][0]
16+
if restrictions[i][1] > restrictions[i-1][1]+dist {
17+
restrictions[i][1] = restrictions[i-1][1] + dist
18+
}
19+
}
20+
21+
for i := m - 2; i >= 0; i-- {
22+
dist := restrictions[i+1][0] - restrictions[i][0]
23+
if restrictions[i][1] > restrictions[i+1][1]+dist {
24+
restrictions[i][1] = restrictions[i+1][1] + dist
25+
}
26+
}
27+
28+
ret := 0
29+
30+
for i := 0; i < m-1; i++ {
31+
id1, h1 := restrictions[i][0], restrictions[i][1]
32+
id2, h2 := restrictions[i+1][0], restrictions[i+1][1]
33+
34+
peak := (h1 + h2 + (id2 - id1)) / 2
35+
ret = max(ret, peak)
36+
}
37+
38+
lastId := restrictions[m-1][0]
39+
lastH := restrictions[m-1][1]
40+
ret = max(ret, lastH+(n-lastId))
41+
42+
return ret
543
}

leetcode/1801-1900/1840.Maximum-Building-Height/Solution_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,23 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
n int
14+
restrictions [][]int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 5, [][]int{{2, 1}, {4, 1}}, 2},
18+
{"TestCase2", 6, [][]int{}, 5},
19+
{"TestCase3", 10, [][]int{{5, 3}, {2, 5}, {7, 4}, {10, 3}}, 5},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.n, c.restrictions)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.n, c.restrictions)
2829
}
2930
})
3031
}

0 commit comments

Comments
 (0)