c++ 99.8% runtime 99.7% memory
The idea is to dry a lake in whch rain will occur in the near future. For example [1,2,0,0,2,1] here at first we dry lake 2 as it is used in the near future.Bru
The idea is to dry a lake in whch rain will occur in the near future. For example [1,2,0,0,2,1] here at first we dry lake 2 as it is used in the near future.Brute force can be done using 2 for loops O(n^2)\n\nOptimised Solution\nuse map to record full lakes and use count is used to know how many subarray of zero exist and vector v to know first and last index of that subarray. For example [1,2,0,0,0,4,5,6,0,0,7,8,0,9] here first 3 zeros belong to one subarray and second 2 to another. while iterating the array when 0 occurs update the subarray and in case non zero element of empty \nlake update the map . if in case of non-empty lake look  for a day to dry if it exists update ans and update map\n\n\n```\nclass Solution {\npublic:\n    vector<int> avoidFlood(vector<int>& A) {\n        int n = A.size();\n        unordered_map<int,int>m;\n        vector<pair<int,int>>v; int count=0;\n        for(int i=0;i<n;i++)\n        {\n            if(A[i]==0)\n            {\n                if(i==0) {A[i]=1;continue;}\n                int j=i;\n                while(j<n && A[j]==0) {  A[j]=1;j++;}\n                count++; \n                v.push_back({i,j});\n                i=j-1;\n            }\n            else\n            {\n                if(m.find(A[i])==m.end())\n                {\n                    \n                    m[A[i]]=count;\n                    A[i]=-1;\n                    \n                }\n                else\n                {\n                    int d =m[A[i]];\n                    while(d<v.size() && v[d].first>=v[d].second) d++;\n                    if(d<v.size() && v[d].first<v[d].second)\n                    {\n                        A[v[d].first]=A[i];\n                        v[d].first++;\n                        m[A[i]]=count;\n                        A[i]=-1;\n                    }\n                    else \n                    { return {};}\n                    \n                }\n               \n            }\n        }\n        return A;\n        \n    }\n};\n```
[]