Ruby 584ms (100% runtime) w/comments - dry queue and binary search
\n# @param {Integer[]} rains\n# @return {Integer[]}\ndef avoid_flood(rains)\n  last_flood = {} # key - lake ID, value - last time when it was flooded\n  dry_que
```\n# @param {Integer[]} rains\n# @return {Integer[]}\ndef avoid_flood(rains)\n  last_flood = {} # key - lake ID, value - last time when it was flooded\n  dry_queue = [] # list of days when we can dry lakes\n  \n  days = [] # result\n  \n  rains.each_with_index do |lake, day|\n    if lake > 0 # if it is raining on that date\n      if last_flood.key?(lake) # if the lake is already full\n        return [] if dry_queue.empty? # if there are no days when we can dry out the lake, then flooding is inavoidable\n        \n\t\t# we need to pick a good day to dry out our lake among all available\n\t\t# it is the very first day after the last time this lake was last flooded\n\t\t# e.g. if the lake was flooded last time on day 30, then let\'s find the earliest possible day after day 30, which is still available\n\t\t# since we know that the array is in ascending order, we can use binary search to achieve O(log N) here\n        day_to_dry_index = dry_queue.bsearch_index {|x| x > last_flood[lake]}\n        \n\t\t# if we couldn\'t find a good day, then it means that even though we have a pool of days when we can dry out lakes\n\t\t# all dry days happened before the last time the current lake was flooded, so there is nothing we can do\n        return [] unless day_to_dry_index\n        \n\t\t# great, we found a good day, let\'s backdate drying the lake on day day_to_dry and remove it from the queue\n        day_to_dry = dry_queue[day_to_dry_index]\n        dry_queue.delete_at(day_to_dry_index)\n        days[day_to_dry] = lake\n      end\n      \n\t  # today, we cannot do anything, because the lake has flooded, just add -1 to the answer and update the last time the lake was flooded\n      last_flood[lake] = day\n      days << -1\n    else # if it is a dry day\n      dry_queue << day # add it to the drying queue\n      days << 1 # add "1" to the answer. it will be backdated and updated later. if we don\'t need this drying day, then it will be just 1, which is OK\n    end\n  end\n  \n  days\nend\n```
[]