upper_bound+set explained c++....
class Solution {\npublic:\n    vector avoidFlood(vector& rains) {\n      \n      int n=rains.size();\n      vector res(n,1);\n      \n      set dry_day;// to st
class Solution {\npublic:\n    vector<int> avoidFlood(vector<int>& rains) {\n      \n      int n=rains.size();\n      vector<int> res(n,1);\n      \n      set<int> dry_day;// to store index where drying of lake can take plase..\n      \n      unordered_map<int,int> memo;//to store the lake which are filled till now..\n      unordered_map<int,int> previous_index;//to store the last index of filled lake  \n      \n      for(int i=0;i<n;i++)\n      {\n        int lake=rains[i];\n        \n        \n        if(lake==0)\n        {\n          \n          dry_day.insert(i);//if its a dry _day store this indeex in set dry_day.\n        }\n        else if(memo[lake]==0)\n        {\n          memo[lake]=1;      // mark the  current lake as filled ans update res[i]=-1 as given in question...\n          res[i]=-1;\n          previous_index[lake]=i;//also store previous index[lake]=i,as when we will get same lake again in fill state we can get this index..\n        }\n        \n        else\n        {\n           auto it=dry_day.upper_bound(previous_index[lake]);//we found a lake to be filled which is already filled search the index just greter than previous index where this lake was filled..this is optimal because if we use other day which is a dryring day then it may cause mismanagement of drying day and flood may occur\n          \n          if(it==dry_day.end()) return {};//if no such index is found\n          \n         //int  index=it-dry_day.begin();\n          res[*it]=lake;// so index *it is the day when we used to empty lake which we dealing now..\n          //memo[lake]=1;\n          dry_day.erase(it);//since this dryring is used day removed it (removal in set takes less time as in vector)...\n          res[i]=-1;\n          previous_index[lake]=i;\n        }\n        \n      }\n      \n      return res;\n        \n    }\n};
[]