[Python/Java] Python bisect / Java TreeSet solution
python\n# yes, I know, it\'s O(n^2), QAQ\n# But If we have treeSet in Python, it\'s will become O(nlogn), as slots.pop(idx) can become O(log(n))\nimport bisect\
```python\n# yes, I know, it\'s O(n^2), QAQ\n# But If we have treeSet in Python, it\'s will become O(nlogn), as slots.pop(idx) can become O(log(n))\nimport bisect\nclass Solution:\n    def avoidFlood(self, rains: List[int]) -> List[int]:\n        res = [-1]*len(rains)\n        slots = []\n        memo = {}\n        for i,r in enumerate(rains):\n            if r==0:\n                slots.append(i)\n            else:\n                if r in memo:\n                    idx = bisect.bisect_left(slots,memo[r])\n                    if idx == len(slots): return []\n                    res[slots.pop(idx)] = r\n                memo[r] = i\n        while slots:\n            res[slots.pop()] = 1\n        return res\n```\n\n```java\n\n// In comparision, java can achieve O(nlogn) with TreeSet, as you can see.\n\nclass Solution {\n    public int[] avoidFlood(int[] rains) {\n        Map<Integer, Integer> posMemo = new HashMap<>();\n        TreeSet<Integer> slots = new TreeSet<>();\n        int[] res = new int[rains.length];\n        Arrays.fill(res,-1);\n        for (int i=0;i<rains.length;i++){\n            if (rains[i]==0){\n                slots.add(i);\n            } else {\n                if (posMemo.containsKey(rains[i])){\n                    Integer slot = slots.ceiling(posMemo.get(rains[i]));\n                    if (slot == null) return new int[0];\n                    res[slot] = rains[i];\n                    slots.remove(slot);\n                }\n                posMemo.put(rains[i],i);\n            }\n        }\n        for (int s: slots) res[s] = 1;\n        return res;\n    }\n}\n\n```
[]