Greedy + Easy Implementation, C++ ✅✅
Approach\n Describe your approach to solving the problem. \nThe approach is simple, we\'ll assign dry days to the lake that rained most recently (2 times).\nFor
# Approach\n<!-- Describe your approach to solving the problem. -->\nThe approach is simple, **we\'ll assign dry days to the lake that rained most recently (2 times)**.\n**For Example-1:- Rains[] = [1,2,0,3,2,1]** Here dry day (2nd day) will be assigned to Lake-2 as it is the first lake where it rained second time.\n\n**For eg-1:- rain[] = [1,2,0,3,1,4]** Here the dry day (2nd day) will be assigned to lake-1 as it is the first lake where it rained second time.\n\n**Note:-**\ntry to implement this approach yourself... (**use list or deque to store dry day position**) if you fail to implement this **please read the code and if it helps you Upvote me please....**\n\n\n# Complexity\n- Time complexity:O(N*M) \n- here M = Number of Dry Day.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    vector<int> avoidFlood(vector<int>& r) {\n        list<int> zero_pos;\n        int n =  r.size();\n        vector<int> ans(n,-1);\n        unordered_map<int,int> freq;\n\n        for(int i = 0;i<n;i++)\n        {\n            if(r[i]==0)\n            zero_pos.push_back(i);\n            else\n            {\n                if(freq.find(r[i])==freq.end())\n                freq[r[i]]  = i;\n                else\n                {\n                    vector<int> tmp;\n                    while(zero_pos.size()>0 && zero_pos.front()<freq[r[i]])\n                    {\n                        tmp.push_back(zero_pos.front());\n                        zero_pos.pop_front();\n                    }\n                    if(zero_pos.size()==0) return {};\n                    else\n                    {\n                        ans[zero_pos.front()] = r[i];\n                        zero_pos.pop_front();\n                        freq[r[i]] = i;\n                        for(int j = tmp.size()-1;j>=0;j--) zero_pos.push_front(tmp[j]);\n                    }\n                }\n            }\n        }\n        while(!zero_pos.empty())\n        {\n            ans[zero_pos.front()] = 1;\n            zero_pos.pop_front();\n        }\n        return ans;\n        \n    }\n};\n```\n![upvote.jpg](https://assets.leetcode.com/users/images/03cdba68-896f-4d72-addb-14f3b2c093de_1688960445.6739316.jpeg)\n
['Greedy', 'C++']