<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 that is <strong>full of water</strong>, there will be a <strong>flood</strong>. Your goal is to avoid floods in any lake.</p>

<p>Given an integer array <code>rains</code> where:</p>

<ul>
	<li><code>rains[i] &gt; 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] &gt; 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.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> rains = [1,2,3,4]
<strong>Output:</strong> [-1,-1,-1,-1]
<strong>Explanation:</strong> 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&#39;s no day to dry any lake and there is no flood in any lake.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> rains = [1,2,0,0,2,1]
<strong>Output:</strong> [-1,-1,2,1,-1,-1]
<strong>Explanation:</strong> 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.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> rains = [1,2,0,1,2]
<strong>Output:</strong> []
<strong>Explanation:</strong> 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&#39;s easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= rains.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= rains[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def dryLake(self, floodedLakes, filledLakes):
        if len(floodedLakes) == 0 or len(filledLakes) == 0:
            return 1 # Dry any arbitrary lake

        indexToRemove = -1
        lakeToDry = 1
        for i, toBeFlooded in enumerate(floodedLakes):
            if toBeFlooded in filledLakes.keys() and filledLakes[toBeFlooded] > 0:
                 indexToRemove = i
                 lakeToDry = toBeFlooded
                 break
        
        if indexToRemove != -1:
            del floodedLakes[indexToRemove]
        
        return lakeToDry

    def avoidFlood(self, rains: List[int]) -> List[int]:
        floodedLakes = deque()
        filledLakes = set()
        for rainyDay in rains:
            if rainyDay == 0:
                continue
            
            if rainyDay in filledLakes:
                floodedLakes.append(rainyDay)
            filledLakes.add(rainyDay)

        ans = []
        filledLakes = {}
        for rainyDay in rains:
            if rainyDay == 0:
                # Empty one of the to-be-flooded lakes
                lakeToDry = self.dryLake(floodedLakes, filledLakes)
                filledLakes[lakeToDry] = 0
                ans.append(lakeToDry)

            else:
                if rainyDay in filledLakes.keys() and filledLakes[rainyDay] > 0:
                    return []
                else:
                    filledLakes[rainyDay] = 1
                ans.append(-1)
        
        return ans

        # DIDN'T WORK
        # dryDayIndices = deque()
        # filledLakes = set()
        # ans = []

        # for i, lakeToFill in enumerate(rains):
        #     if lakeToFill == 0:
        #         dryDayIndices.append(i)

        #         # Append a placeholder, it doesn't matter which lake we use for now.
        #         ans.append(1)
        #         continue

        #     if lakeToFill in filledLakes:
        #         if len(dryDayIndices) > 0:
        #             dryDay = dryDayIndices.popleft()
        #             ans[dryDay] = lakeToFill
        #             ans.append(-1)
        #         else:
        #             # We don't have a way to avoid a flood.
        #             return []
        #     else:
        #         filledLakes.add(lakeToFill)
        #         ans.append(-1)
        
        # return ans

"""
Each i represents a day
rains[i] represents the lake that it will rain in

If rains[i] == 0, we need to dry a lake

How to know which lake to dry when it's a day to prevent flooding?

One pass to see which lakes get filled more than once.
Keep track of lakes that would flood in the order they would flood.
Go back through rains and see if drying lakes will help
Ex 3: [1, 2]

Ex 4: [1, 2, 0, 1, 1]
[1, 1]

Keep track of number of dry days.
Keep track of how many lakes will flood and see if we have enough dry days.
"""
        
33500