Created
January 22, 2022 00:48
-
-
Save germanescobar/062262aa4c1dd52d8a563c7c0b7fb693 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var avoidFlood = function(rains) { | |
const result = [] | |
const filled = new Set() | |
return findSolution(rains, filled, result, 0) | |
}; | |
function findSolution(rains, filled, result, day) { | |
if (day >= rains.length) { // lo logramos! | |
return result | |
} | |
if (rains[day] > 0 && filled.has(rains[day])) { // inundación!!!!! | |
return [] | |
} | |
if (rains[day] > 0) { // ese día llovió | |
result[day] = -1 | |
filled.add(rains[day]) | |
return findSolution(rains, filled, [...result], day + 1) | |
} else { // no llovió, tenemos que secar un lago | |
if (filled.size === 0) { // no hay lagos llenos | |
result[day] = 1 | |
return findSolution(rains, filled, [...result], day + 1) | |
} else { // probar secando cada lago | |
for (lake of filled) { | |
const lakes = new Set(filled) | |
lakes.delete(lake) // lo vamos a secar | |
result[day] = lake | |
const s = findSolution(rains, lakes, [...result], day + 1) | |
if (s.length > 0) return s | |
} | |
return [] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ya encontré el error, faltaba actualizar el último día de lluvia de ese lago!