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] &gt; 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] &gt; 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.

&nbsp;
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.


&nbsp;
Constraints:


	1 &lt;= rains.length &lt;= 105
	0 &lt;= rains[i] &lt;= 109


from bisect import bisect_left

class Solution:
    def avoidFlood(self, rains):
        full_lakes, dry_dates = {}, []
        ans = [-1] * len(rains)

        for date, rain_lake in enumerate(rains):
            if rain_lake == 0:  # no rain, we can dry one lake
                dry_dates.append(date)  # keep dry date, we'll decide later
                continue

            if rain_lake in full_lakes:  # the lake is already full
                # BS find out earliest day we can use to dry that lake | greedy
                dry_day = bisect_left(dry_dates, full_lakes[rain_lake])

                if dry_day >= len(dry_dates): return []  # can not find a date to dry this lake

                ans[dry_dates.pop(dry_day)] = rain_lake  # dry this lake at the date we choose

            # remember latest rain on this lake
            full_lakes[rain_lake] = date

        # we may have dry dates remain, on these days, rain > 0, we can not use -1, just choose day 1 to dry (maybe nothing happend)
        for dry_day in dry_dates:
            ans[dry_day] = 1

        return ans
class Solution {
    public int[] avoidFlood(int[] rains) {
        // the previous appeared idx of rains[i]
        Map<Integer, Integer> map = new HashMap<>();
        TreeSet<Integer> zeros = new TreeSet<>();
        int[] res = new int[rains.length];
        for (int i = 0; i < rains.length; i++) {
            if (rains[i] == 0) {
                zeros.add(i);
            } else {
                if (map.containsKey(rains[i])) {
                    // find the location of zero that can be used to empty rains[i]
                    Integer next = zeros.ceiling(map.get(rains[i]));
                    if (next == null) return new int[0];
                    res[next] = rains[i];
                    zeros.remove(next);
                }
                res[i] = -1;
				map.put(rains[i], i);
            }
        }
        for (int i : zeros) res[i] = 1;
        return res;
    }
}
class Solution {
public:
    vector<int> avoidFlood(vector<int>& rains) {
        vector<int> ans(rains.size() , -1) ;
        unordered_map<int,int> indices ; //store the lake and its index 
        set<int> st ; //stores the indices of zeros 
        
        for(int i = 0 ; i < rains.size() ; ++i ){
            if(!rains[i]) st.insert(i) ;
            else{
                if(indices.find(rains[i]) == end(indices)) indices[rains[i]] = i ;
                else{
                    int prevDay = indices[rains[i]] ;
                    auto it = st.upper_bound(prevDay) ;
                    if(it == end(st)) return {} ;
                    ans[*it] = rains[i] ;
                    indices[rains[i]] = i ;
                    st.erase(it);
                }
            }
        }
        
        for(int i = 0 ; i < ans.size(); ++i ){
            if(!rains[i] and ans[i] == -1) ans[i] = 1 ;
        }
        return ans ;
    }
};
var avoidFlood = function(rains) {
    const n = rains.length;
    const filledLakes = new Map();
    const res = new Array(n).fill(-1);
    const dryDays = [];

    for (let i = 0; i < n; i++) {
        const lake = rains[i]; // lake to rain on

        if (lake === 0) {
        // It is a dry day
            dryDays.push(i);
        }
        else if (!filledLakes.has(lake)) {
        // The lake is not filled yet, so we let it be filled (we just don't want it to be rained on again and be flooded)
            filledLakes.set(lake, i);
        }
        else {
        // The lake is already filled. We want to see if a dry day was available after the lake was previously rained on so that we can empty the lake
            const lake_index = filledLakes.get(lake); //
            const dry_index = binarySearch(lake_index);

            if (dry_index === dryDays.length) return []; // there was no dry day after the lake was previouly filled

            res[dryDays[dry_index]] = lake; // mark the earliest dry day that was used in our result array
            filledLakes.set(lake, i); // we need to update the day that the lake is rained on again
            dryDays.splice(dry_index, 1); // remove the dry day that was used (this is not very efficient, but it just makes our code cleaner)
        }
    }

    dryDays.forEach((day) => res[day] = 1);

    return res;

    function binarySearch(target) {
        let left = 0;
        let right = dryDays.length - 1;

        while (left <= right) {
            const mid = left + Math.floor((right - left) / 2);

            if (dryDays[mid] < target) left = mid + 1;
            else right = mid - 1;
        }

        return left;
    }
};
Avoid Flood in The City