Javascript. with comments and refactored wtih hashSet and map
\n// if rains[i] > 0, ans[i] = -1\n// for every repeat rains[i] (lake), there has to be a 0 in between repeats\n// ORDER MATTERS\nvar avoidFlood = function(rain
```\n// if rains[i] > 0, ans[i] = -1\n// for every repeat rains[i] (lake), there has to be a 0 in between repeats\n// ORDER MATTERS\nvar avoidFlood = function(rains) {\n    let stackOfZeros = []\n    let ans = new Array(rains.length).fill(-1)\n    \n    let fullLakes = new Map() // {lake: index}\n    let dryDays = new Set() // [indices with 0]\n    for (let i = 0; i < rains.length; i++){\n        if (rains[i] === 0) {\n            dryDays.add(i)\n            ans[i] = 0\n        } else {\n            if (!fullLakes.has(rains[i])) { // set {lake => index}\n                fullLakes.set(rains[i], i)\n            } else { // lake is full, check if a 0 is between current i and prev index\n                let previous = fullLakes.get(rains[i])\n                if (dryDays.size === 0) return []\n                let dryLake = false // see if the lake can be dried\n                for(let day of dryDays.keys()){\n                    if (previous < day && day < i){\n                        ans[day] = rains[i] // dry the lake\n                        dryDays.delete(day) // remove the 0\n                        fullLakes.set(rains[i], i) // reset when lake is refilled\n                        dryLake = true // able to dry a lake\n                        break\n                    } \n                }\n                if (!dryLake){ // couldn\'t dry\n                    return []\n                }\n            }\n        }\n    }\n    // remaining 0s get assigned \n    for (let i = 0; i < ans.length; i++){\n        if (ans[i] === 0) {\n            ans[i] = 1\n        }\n    }\n    return ans\n}\n```
[]