C++ with comments
\n    vector<int> avoidFlood(vector<int>& rains) {\n        vector<int> ans(rains.size(), -1);\n        // hashmap to hold the index of previous rainy days\n   
```\n    vector<int> avoidFlood(vector<int>& rains) {\n        vector<int> ans(rains.size(), -1);\n        // hashmap to hold the index of previous rainy days\n        std::unordered_map<int, int> previous;\n        // Set to store the dry days index\n        std::set<int> dryDays;\n        \n        for (int i = 0; i < rains.size(); i++) {\n            if (rains[i] == 0) {\n                // No rain this day, a dry day, save it in the\n                // set and will use later\n                dryDays.insert(i);\n                // Store 1, in case if we choose to dry an empty lake\n                ans[i] = 1;\n            }\n            else {\n                if (previous.count(rains[i]) > 0) {\n                    // The lake was already filled, need to see if there\n                    // is any dry lake, after the last filled lake\n                    auto it = dryDays.upper_bound(previous[rains[i]]);\n                    // If there is no dry days, we can\'t avoid the flood,\n                    // returning empty array\n                    if (it == dryDays.end())\n                        return {};\n                    \n                    // empty the lake that was previously filled\n                    ans[*it] = rains[i];\n                    // Erase the used dry day\n                    dryDays.erase(it);\n                }\n                // store the index of the lake in the hash map\n                previous[rains[i]] =  i;\n            }\n        }\n        return ans;       \n    }\n```
[]