✅ Typescript solution O(n log n). Beats 100% time and space
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n log n)\n\n- Space complexity:\nO(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nfunction getFirstIndexGreater(arr: number[], k: number): number | undefined {\n    if (arr.length === 0) {\n        return undefined\n    }\n    if (k > arr[arr.length - 1]) {\n        return undefined\n    }\n\n    let low = 0\n    let high = arr.length - 1\n\n    while(low <= high) {\n        const mid = low + Math.floor((high - low) / 2)\n        if (arr[mid] > k) {\n            high = mid - 1\n        } else {\n            low = mid + 1\n        }\n    }\n\n    return low\n\n}\n\n\nfunction avoidFlood(rains: number[]): number[] {\n    const filledLakesMap = new Map<number, number>()\n    const dryDays = []\n    const result = []\n\n    for (let i = 0; i < rains.length; i++) {\n        if (rains[i] === 0) {\n            dryDays.push(i)\n            result.push(1)\n        } else {\n            if (!filledLakesMap.has(rains[i])) {\n                result.push(-1)\n                filledLakesMap.set(rains[i], i)\n            } else {\n                const dryIndex = getFirstIndexGreater(dryDays, filledLakesMap.get(rains[i]))\n                \n                if (dryIndex !== undefined) {\n                    const [dryDay] = dryDays.splice(dryIndex, 1)\n                    result[dryDay] = rains[i]\n                    filledLakesMap.set(rains[i], i)\n                    result.push(-1)\n                } else {\n                    return []\n                }\n            }\n        }\n    }\n    return result\n}\n\n\n```
['TypeScript']