Skip to content

Commit 1202d71

Browse files
authored
Merge pull request #1336 from 0xff-dev/1488
Add solution and test-cases for problem 1488
2 parents b3189cd + 582e5e9 commit 1202d71

File tree

3 files changed

+141
-8
lines changed

3 files changed

+141
-8
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# [1488.Avoid Flood in The City][title]
2+
3+
## Description
4+
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the `nth` lake, the `nth` lake becomes **full of water**. If it rains over a lake that is full of water, there will be a **flood**. Your goal is to avoid floods in any lake.
5+
6+
Given an integer array `rains` where:
7+
8+
- `rains[i] > 0` means there will be rains over the `rains[i]` lake.
9+
- `rains[i] == 0` means there are no rains this day and you can choose **one lake** this day and **dry it**.
10+
11+
Return an array `ans` where:
12+
13+
- `ans.length == rains.length`
14+
- `ans[i] == -1` if `rains[i] > 0`.
15+
- `ans[i]` is the lake you choose to dry in the `ith` day if `rains[i] == 0`.
16+
17+
If there are multiple valid answers return **any** of them. If it is impossible to avoid flood return **an empty array**.
18+
19+
Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes.
20+
21+
**Example 1:**
22+
23+
```
24+
Input: rains = [1,2,3,4]
25+
Output: [-1,-1,-1,-1]
26+
Explanation: After the first day full lakes are [1]
27+
After the second day full lakes are [1,2]
28+
After the third day full lakes are [1,2,3]
29+
After the fourth day full lakes are [1,2,3,4]
30+
There's no day to dry any lake and there is no flood in any lake.
31+
```
32+
33+
**Example 2:**
34+
35+
```
36+
Input: rains = [1,2,0,0,2,1]
37+
Output: [-1,-1,2,1,-1,-1]
38+
Explanation: After the first day full lakes are [1]
39+
After the second day full lakes are [1,2]
40+
After the third day, we dry lake 2. Full lakes are [1]
41+
After the fourth day, we dry lake 1. There is no full lakes.
42+
After the fifth day, full lakes are [2].
43+
After the sixth day, full lakes are [1,2].
44+
It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario.
45+
```
46+
47+
**Example 3:**
48+
49+
```
50+
Input: rains = [1,2,0,1,2]
51+
Output: []
52+
Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day.
53+
After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood.
54+
```
55+
56+
## 结语
57+
58+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
59+
60+
[title]: https://leetcode.com/problems/avoid-flood-in-the-city
61+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,77 @@
11
package Solution
22

3-
func Solution(x bool) bool {
3+
import "container/heap"
4+
5+
// 记录未来即将出现的lake
6+
type lakeState struct {
7+
lake int
8+
nextDay int
9+
}
10+
11+
type lakeHeap struct {
12+
data []*lakeState
13+
}
14+
15+
func (l *lakeHeap) Len() int {
16+
return len(l.data)
17+
}
18+
19+
func (l *lakeHeap) Swap(i, j int) {
20+
l.data[i], l.data[j] = l.data[j], l.data[i]
21+
}
22+
23+
func (l *lakeHeap) Less(i, j int) bool {
24+
a, b := l.data[i], l.data[j]
25+
return a.nextDay < b.nextDay
26+
}
27+
28+
func (l *lakeHeap) Push(x any) {
29+
l.data = append(l.data, x.(*lakeState))
30+
}
31+
32+
func (l *lakeHeap) Pop() any {
33+
ll := len(l.data)
34+
x := l.data[ll-1]
35+
l.data = l.data[:ll-1]
436
return x
537
}
38+
39+
func Solution(rains []int) []int {
40+
l := len(rains)
41+
days := make(map[int][]int)
42+
// 记录走到那一天了
43+
indies := make(map[int]int)
44+
for index, rain := range rains {
45+
if rain == 0 {
46+
continue
47+
}
48+
days[rain] = append(days[rain], index)
49+
indies[rain] = 0
50+
}
51+
full := make(map[int]bool)
52+
h := &lakeHeap{data: make([]*lakeState, 0)}
53+
ret := make([]int, l)
54+
for index, lake := range rains {
55+
if lake > 0 {
56+
if full[lake] {
57+
// 满
58+
return []int{}
59+
}
60+
full[lake] = true
61+
ret[index] = -1
62+
if nextIndex := indies[lake] + 1; nextIndex < len(days[lake]) {
63+
indies[lake]++
64+
heap.Push(h, &lakeState{lake: lake, nextDay: days[lake][nextIndex]})
65+
}
66+
continue
67+
}
68+
ret[index] = 1
69+
if h.Len() > 0 {
70+
top := heap.Pop(h).(*lakeState)
71+
ret[index] = top.lake
72+
delete(full, top.lake)
73+
}
74+
}
75+
// 1, 2, 0, 2, 3, 0, 1
76+
return ret
77+
}

leetcode/1401-1500/1488.Avoid-Flood-in-The-City/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1, 2, 3, 4}, []int{-1, -1, -1, -1}},
17+
{"TestCase2", []int{1, 2, 0, 0, 2, 1}, []int{-1, -1, 2, 1, -1, -1}},
18+
{"TestCase3", []int{1, 2, 0, 1, 2}, []int{}},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)