|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <openset.wang@gmail.com> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](../making-file-names-unique "Making File Names Unique") |
| 9 | + |
| 10 | +[Next >](../find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree "Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree") |
| 11 | + |
| 12 | +## [1488. Avoid Flood in The City (Medium)](https://leetcode.com/problems/avoid-flood-in-the-city "避免洪水泛滥") |
| 13 | + |
| 14 | +<p>Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the <code>nth</code> lake, the <code>nth</code> lake becomes full of water. If it rains over a lake which is <strong>full of water</strong>, there will be a <strong>flood</strong>. Your goal is to avoid the flood in any lake.</p> |
| 15 | + |
| 16 | +<p>Given an integer array <code>rains</code> where:</p> |
| 17 | + |
| 18 | +<ul> |
| 19 | + <li><code>rains[i] > 0</code> means there will be rains over the <code>rains[i]</code> lake.</li> |
| 20 | + <li><code>rains[i] == 0</code> means there are no rains this day and you can choose <strong>one lake</strong> this day and <strong>dry it</strong>.</li> |
| 21 | +</ul> |
| 22 | + |
| 23 | +<p>Return <em>an array <code>ans</code></em> where:</p> |
| 24 | + |
| 25 | +<ul> |
| 26 | + <li><code>ans.length == rains.length</code></li> |
| 27 | + <li><code>ans[i] == -1</code> if <code>rains[i] > 0</code>.</li> |
| 28 | + <li><code>ans[i]</code> is the lake you choose to dry in the <code>ith</code> day if <code>rains[i] == 0</code>.</li> |
| 29 | +</ul> |
| 30 | + |
| 31 | +<p>If there are multiple valid answers return <strong>any</strong> of them. If it is impossible to avoid flood return <strong>an empty array</strong>.</p> |
| 32 | + |
| 33 | +<p>Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)</p> |
| 34 | + |
| 35 | +<p> </p> |
| 36 | +<p><strong>Example 1:</strong></p> |
| 37 | + |
| 38 | +<pre> |
| 39 | +<strong>Input:</strong> rains = [1,2,3,4] |
| 40 | +<strong>Output:</strong> [-1,-1,-1,-1] |
| 41 | +<strong>Explanation:</strong> After the first day full lakes are [1] |
| 42 | +After the second day full lakes are [1,2] |
| 43 | +After the third day full lakes are [1,2,3] |
| 44 | +After the fourth day full lakes are [1,2,3,4] |
| 45 | +There's no day to dry any lake and there is no flood in any lake. |
| 46 | +</pre> |
| 47 | + |
| 48 | +<p><strong>Example 2:</strong></p> |
| 49 | + |
| 50 | +<pre> |
| 51 | +<strong>Input:</strong> rains = [1,2,0,0,2,1] |
| 52 | +<strong>Output:</strong> [-1,-1,2,1,-1,-1] |
| 53 | +<strong>Explanation:</strong> After the first day full lakes are [1] |
| 54 | +After the second day full lakes are [1,2] |
| 55 | +After the third day, we dry lake 2. Full lakes are [1] |
| 56 | +After the fourth day, we dry lake 1. There is no full lakes. |
| 57 | +After the fifth day, full lakes are [2]. |
| 58 | +After the sixth day, full lakes are [1,2]. |
| 59 | +It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario. |
| 60 | +</pre> |
| 61 | + |
| 62 | +<p><strong>Example 3:</strong></p> |
| 63 | + |
| 64 | +<pre> |
| 65 | +<strong>Input:</strong> rains = [1,2,0,1,2] |
| 66 | +<strong>Output:</strong> [] |
| 67 | +<strong>Explanation:</strong> After the second day, full lakes are [1,2]. We have to dry one lake in the third day. |
| 68 | +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. |
| 69 | +</pre> |
| 70 | + |
| 71 | +<p><strong>Example 4:</strong></p> |
| 72 | + |
| 73 | +<pre> |
| 74 | +<strong>Input:</strong> rains = [69,0,0,0,69] |
| 75 | +<strong>Output:</strong> [-1,69,1,1,-1] |
| 76 | +<strong>Explanation:</strong> Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9 |
| 77 | +</pre> |
| 78 | + |
| 79 | +<p><strong>Example 5:</strong></p> |
| 80 | + |
| 81 | +<pre> |
| 82 | +<strong>Input:</strong> rains = [10,20,20] |
| 83 | +<strong>Output:</strong> [] |
| 84 | +<strong>Explanation:</strong> It will rain over lake 20 two consecutive days. There is no chance to dry any lake. |
| 85 | +</pre> |
| 86 | + |
| 87 | +<p> </p> |
| 88 | +<p><strong>Constraints:</strong></p> |
| 89 | + |
| 90 | +<ul> |
| 91 | + <li><code>1 <= rains.length <= 10^5</code></li> |
| 92 | + <li><code>0 <= rains[i] <= 10^9</code></li> |
| 93 | +</ul> |
| 94 | + |
| 95 | +### Related Topics |
| 96 | + [[Array](../../tag/array/README.md)] |
| 97 | + [[Hash Table](../../tag/hash-table/README.md)] |
| 98 | + |
| 99 | +### Hints |
| 100 | +<details> |
| 101 | +<summary>Hint 1</summary> |
| 102 | +Keep An array of the last day there was rains over each city. |
| 103 | +</details> |
| 104 | + |
| 105 | +<details> |
| 106 | +<summary>Hint 2</summary> |
| 107 | +Keep an array of the days you can dry a lake when you face one. |
| 108 | +</details> |
| 109 | + |
| 110 | +<details> |
| 111 | +<summary>Hint 3</summary> |
| 112 | +When it rains over a lake, check the first possible day you can dry this lake and assign this day to this lake. |
| 113 | +</details> |
0 commit comments