java.lang.Object
g1401_1500.s1488_avoid_flood_in_the_city.Solution

public class Solution extends Object
1488 - Avoid Flood in The City.<p>Medium</p> <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> <p>Given an integer array <code>rains</code> where:</p> <ul> <li><code>rains[i] > 0</code> means there will be rains over the <code>rains[i]</code> lake.</li> <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> </ul> <p>Return <em>an array <code>ans</code></em> where:</p> <ul> <li><code>ans.length == rains.length</code></li> <li><code>ans[i] == -1</code> if <code>rains[i] > 0</code>.</li> <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> </ul> <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> <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> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> rains = [1,2,3,4]</p> <p><strong>Output:</strong> [-1,-1,-1,-1]</p> <p><strong>Explanation:</strong> After the first day full lakes are [1]</p> <p>After the second day full lakes are [1,2]</p> <p>After the third day full lakes are [1,2,3]</p> <p>After the fourth day full lakes are [1,2,3,4]</p> <p>There&rsquo;s no day to dry any lake and there is no flood in any lake.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> rains = [1,2,0,0,2,1]</p> <p><strong>Output:</strong> [-1,-1,2,1,-1,-1]</p> <p><strong>Explanation:</strong> After the first day full lakes are [1]</p> <p>After the second day full lakes are [1,2]</p> <p>After the third day, we dry lake 2. Full lakes are [1]</p> <p>After the fourth day, we dry lake 1. There is no full lakes.</p> <p>After the fifth day, full lakes are [2].</p> <p>After the sixth day, full lakes are [1,2].</p> <p>It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> rains = [1,2,0,1,2]</p> <p><strong>Output:</strong> []</p> <p><strong>Explanation:</strong> After the second day, full lakes are [1,2]. We have to dry one lake in the third day.</p> <p>After that, it will rain over lakes [1,2]. It&rsquo;s easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= rains.length <= 10<sup>5</sup></code></li> <li><code>0 <= rains[i] <= 10<sup>9</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • avoidFlood

      public int[] avoidFlood(int[] rains)