JAVA || Binary Search || HashMap || easy to understand
\nclass Solution {\n    int getNearestDryDay(List<Integer> dryDays, int targetDay) //returns the nearest dry day after targetDay\n    {\n        int low = 0, hi
```\nclass Solution {\n    int getNearestDryDay(List<Integer> dryDays, int targetDay) //returns the nearest dry day after targetDay\n    {\n        int low = 0, high = dryDays.size() - 1;\n        int ans = -1, index = -1;\n        while(low <= high)\n        {\n            int mid = low + (high - low) / 2;\n            int currDay = dryDays.get(mid);\n            if (currDay > targetDay) { index = mid; ans = currDay; high = mid - 1; }\n            else low = mid + 1;\n        }\n        if (index != -1) dryDays.remove(index);\n        return ans;\n    }\n    public int[] avoidFlood(int[] rains) \n    {\n        HashMap<Integer, Integer>fullMap = new HashMap(); //{lakeIdx : day} \n        int n = rains.length;\n        int[] ans = new int[n];\n        Arrays.fill(ans, 1);\n        //=========================================================\n        List<Integer>dryDays = new ArrayList();\n        for (int i = 0; i < n; i++) //i denotes the ith day\n        {\n            int lakeIdx = rains[i]; \n            if (lakeIdx == 0) { dryDays.add(i); continue; }  //no rain over any lake\n            \n            if (fullMap.containsKey(lakeIdx)) //if current lake is already full\n            {\n                int lastDrenchedDay = fullMap.get(lakeIdx); //day when "lakeIdx"th lake was last drenched\n                int nearestDryDay = getNearestDryDay(dryDays, lastDrenchedDay);\n                \n                if (nearestDryDay == -1) return new int[]{};\n                ans[nearestDryDay] = lakeIdx;\n            }\n            fullMap.put(lakeIdx, i);\n            ans[i] = -1;\n        }\n        //===========================================================\n        return ans;\n    }\n}\n```
['Binary Tree', 'Java']