Solución
solution.tsTypeScript
function trapWater(heights: number[]): number {
// TODO: Implementa tu solución aquí
let ultimaAltura = heights[0] ?? 0
let tempAgua = 0;
let totalAgua = 0;
const max = Math.max(...heights)
heights.forEach(i => {
if (i >= ultimaAltura) {
totalAgua += tempAgua;
tempAgua = 0;
if (i < max) {
ultimaAltura = i;
}
} else {
tempAgua += ultimaAltura - i;
}
});
return totalAgua;
}
// No modificar: necesario para evaluar el resultado.
export { trapWater };0respuestas