C++ , very brief explanation
Intuition:\n\nBrute force : whenever we encounter rains[i] = 0 ,  we try to find the first lake which we can empty ,that was already filled before we encountere
Intuition:\n\nBrute force : whenever we encounter rains[i] = 0 ,  we try to find the **first lake** which we can empty ,that was already filled before we encountered rains[i] = 0. It would take O(n) for every rains[i] = 0, to find the lake which we should empty,  so overall complexity is O(n^2).\n\nOptimized : We need to optimize the answer by able to find the correct lake which needs to be emptied. So we can use **min heap**, to keep track of the first lake\'s index which is already filled.\n\n\n```\nstruct myComp {\n    bool operator()(pair<int,int> p1 , pair<int,int> p2) {\n        return p1.first > p2.first;\n    }\n};\n\n\nclass Solution {\npublic:\n    vector<int> avoidFlood(vector<int>& rains) {\n        unordered_map<int,pair<int,vector<int>>> umap;\n        unordered_map<int,int> filled;\n        \n        priority_queue<pair<int,int> , vector<pair<int,int>> , myComp> pq;\n        \n        for(int i = 0 ; i < rains.size() ; i++) {\n            umap[rains[i]].first = 1;\n            umap[rains[i]].second.push_back(i);\n        }\n        vector<int> result;\n        for(int i = 0 ; i < rains.size() ;i++) {\n            \n            if(rains[i] == 0) {\n                if(!pq.empty()) {\n                    pair<int,int> temp = pq.top();\n                    pq.pop();\n                    filled[temp.second] = 0;\n                    result.push_back(temp.second);\n                }\n                else {\n                    result.push_back(1);\n                }\n            }\n            else if(filled[rains[i]] == 0) {\n                filled[rains[i]] = 1;\n                int index = umap[rains[i]].first;\n                vector<int> temp = umap[rains[i]].second;\n                if(index < temp.size()) {\n                    pq.push({temp[index],rains[i]});\n                    index++;\n                    umap[rains[i]].first = index;\n                }\n                \n                result.push_back(-1);\n            }\n            else if(filled[rains[i]] != 0){\n                return {};\n            }\n        }\n       return result;\n        \n    }\n};\n\n```\n\n\nPlease comment below if someones wants me to write a detailed explaination
['Greedy', 'Heap (Priority Queue)']