A Set and A Map - Lucid code with documented comments.
\nTwo key things to note-\n1. When drying a lake #L, it is only useful to dry it if it is FULL already. Otherwise its of no use. (Explained this in the code wit
\n**Two key things to note-**\n1. When drying a lake #L, it is only useful to dry it if it is FULL already. Otherwise its of no use. (Explained this in the code with a small example as well.)\n2. Which lake to dry on a day when there is no rain, can not be determined without knowing the rain sequence that is coming next.\nFor example - If you have rains = [1, 2, 0, ??]. Without knowing what the \'??\' is, you can not determine which lake to dry on the 3rd day (rains[2]), this is because if \'??\' = 1, then you must dry the lake #1 to avoid flooding. Similarly, if \'??\' =2,  then you must dry lake #2 to avoid flooding.\n\n![image](https://assets.leetcode.com/users/images/e775a319-756a-4e1c-8a61-2ef448137900_1593072251.1421912.png)\n\nDetailed Comments in the code.\n\nPlease upvote if it helps - \n1. It encourages me to write and share my solutions if they are easy to understand. \n2. It reaches to more learners, larger audience. :)\n\nThanks.\n\nC++\n\n```\nclass Solution {\npublic:\n    vector<int> avoidFlood(vector<int>& rains) {\n        vector<int> ans; // Store the final answer here.\n        int n = rains.size(); \n        unordered_map<int, int> fulllakes; // Lake number -> day on which it became full.\n        set<int> drydays;     // Set of available days that can be used for drying a full lake.\n        for (int i=0; i<n; i++) {  // For each day - \n            if (rains[i] == 0) {  // No rain on this day.\n                drydays.insert(i); // This day can be used as a day to dry some lake.\n                                   // We don\'t know which lake to prioritize for drying yet.\n                ans.push_back(1);  // Any number would be ok. This will get overwritten eventually.\n                                   // If it doesn\'t get overwritten, its totally ok to dry a lake\n                                   // irrespective of whether it is full or empty.\n            } else { // Rained in rains[i]-th lake.\n                int lake = rains[i]; \n                if (fulllakes.find(lake) != fulllakes.end()) { // If it is already full -\n                    // We must dry this lake before it rains in this lake.\n                    // So find a day in "drydays" to dry this lake. Obviously, that day must be\n                    // a day that is after the day on which the lake was full.\n                    // i.e. if the lake got full on 7th day, we must find a dry day that is \n                    // greater than 7.\n                    auto it = drydays.lower_bound(fulllakes[lake]);\n                    if (it == drydays.end()) { // If there is no available dry day to dry the lake,\n                                               // flooding is inevitable.\n                        return {}; // Sorry, couldn\'t stop flooding.\n                    }\n                    int dryday = *it; // Great, we found a day which we can use to dry this lake.\n                    ans[dryday] = lake; // Overwrite the "1" and dry "lake"-th lake instead.\n                    drydays.erase(dryday); // We dried "lake"-th lake on "dryday", and we can\'t use\n                                           // the same day to dry any other lake, so remove the day\n                                           // from the set of available drydays.\n                }\n                fulllakes[lake] = i; // Update that the "lake" became full on "i"-th day.\n                ans.push_back(-1); // As the problem statement expects.\n            }\n        }\n        return ans; // Congratualtions, you avoided flooding.\n    }\n};\n```\n\n\n
['C']