Skip to content
Open
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
61 changes: 61 additions & 0 deletions leetcode/1401-1500/1488.Avoid-Flood-in-The-City/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# [1488.Avoid Flood in The City][title]

## Description
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.

Given an integer array `rains` where:

- `rains[i] > 0` means there will be rains over the `rains[i]` lake.
- `rains[i] == 0` means there are no rains this day and you can choose **one lake** this day and **dry it**.

Return an array `ans` where:

- `ans.length == rains.length`
- `ans[i] == -1` if `rains[i] > 0`.
- `ans[i]` is the lake you choose to dry in the `ith` day if `rains[i] == 0`.

If there are multiple valid answers return **any** of them. If it is impossible to avoid flood return **an empty array**.

Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes.

**Example 1:**

```
Input: rains = [1,2,3,4]
Output: [-1,-1,-1,-1]
Explanation: After the first day full lakes are [1]
After the second day full lakes are [1,2]
After the third day full lakes are [1,2,3]
After the fourth day full lakes are [1,2,3,4]
There's no day to dry any lake and there is no flood in any lake.
```

**Example 2:**

```
Input: rains = [1,2,0,0,2,1]
Output: [-1,-1,2,1,-1,-1]
Explanation: After the first day full lakes are [1]
After the second day full lakes are [1,2]
After the third day, we dry lake 2. Full lakes are [1]
After the fourth day, we dry lake 1. There is no full lakes.
After the fifth day, full lakes are [2].
After the sixth day, full lakes are [1,2].
It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario.
```

**Example 3:**

```
Input: rains = [1,2,0,1,2]
Output: []
Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day.
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.
```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]

[title]: https://leetcode.com/problems/avoid-flood-in-the-city
[me]: https://github.com/kylesliu/awesome-golang-algorithm
74 changes: 73 additions & 1 deletion leetcode/1401-1500/1488.Avoid-Flood-in-The-City/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,77 @@
package Solution

func Solution(x bool) bool {
import "container/heap"

// 记录未来即将出现的lake
type lakeState struct {
lake int
nextDay int
}

type lakeHeap struct {
data []*lakeState
}

func (l *lakeHeap) Len() int {
return len(l.data)
}

func (l *lakeHeap) Swap(i, j int) {
l.data[i], l.data[j] = l.data[j], l.data[i]
}

func (l *lakeHeap) Less(i, j int) bool {
a, b := l.data[i], l.data[j]
return a.nextDay < b.nextDay
}

func (l *lakeHeap) Push(x any) {
l.data = append(l.data, x.(*lakeState))
}

func (l *lakeHeap) Pop() any {
ll := len(l.data)
x := l.data[ll-1]
l.data = l.data[:ll-1]
return x
}

func Solution(rains []int) []int {
l := len(rains)
days := make(map[int][]int)
// 记录走到那一天了
indies := make(map[int]int)
for index, rain := range rains {
if rain == 0 {
continue
}
days[rain] = append(days[rain], index)
indies[rain] = 0
}
full := make(map[int]bool)
h := &lakeHeap{data: make([]*lakeState, 0)}
ret := make([]int, l)
for index, lake := range rains {
if lake > 0 {
if full[lake] {
// 满
return []int{}
}
full[lake] = true
ret[index] = -1
if nextIndex := indies[lake] + 1; nextIndex < len(days[lake]) {
indies[lake]++
heap.Push(h, &lakeState{lake: lake, nextDay: days[lake][nextIndex]})
}
continue
}
ret[index] = 1
if h.Len() > 0 {
top := heap.Pop(h).(*lakeState)
ret[index] = top.lake
delete(full, top.lake)
}
}
// 1, 2, 0, 2, 3, 0, 1
return ret
}
14 changes: 7 additions & 7 deletions leetcode/1401-1500/1488.Avoid-Flood-in-The-City/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []int
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 2, 3, 4}, []int{-1, -1, -1, -1}},
{"TestCase2", []int{1, 2, 0, 0, 2, 1}, []int{-1, -1, 2, 1, -1, -1}},
{"TestCase3", []int{1, 2, 0, 1, 2}, []int{}},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading