SIMPLE JAVA SOLUTION MAP+TREE-SET
JAVA CODE IS:\n# \n\nclass Solution {\n    public int[] avoidFlood(int[] rains) {\n        //map contains pair as values-index\n       Map<Integer,Integer>map=n
JAVA CODE IS:\n# \n```\nclass Solution {\n    public int[] avoidFlood(int[] rains) {\n        //map contains pair as values-index\n       Map<Integer,Integer>map=new HashMap<>();\n        //dry contains index of no rains days\n        TreeSet<Integer>dry=new TreeSet<>();\n        int ans[]=new int[rains.length];\n        for(int i=0;i<rains.length;i++){\n           if(rains[i]==0){\n               dry.add(i);\n               continue;\n           }\n            ans[i]=-1;\n           if(!map.containsKey(rains[i])){ \n               map.put(rains[i],i);\n               continue;\n           }\n           if(dry.ceiling(map.get(rains[i]))==null) return new int[]{};\n           ans[dry.ceiling(map.get(rains[i]))]=rains[i];\n           dry.remove(dry.ceiling(map.get(rains[i])));\n            map.put(rains[i],i);\n        }\n        for(int k : dry) ans[k]=1;\n        return ans;\n    }\n}\n```\nTIME : O(nlogn)\nSPACE : O(n)\n***PLEASE,UPVOTE IF THIS IS HELPFUL***
[]