Created
August 29, 2018 00:45
-
-
Save adleroliveira/acc88444107f7fbb731a612f3060c8e7 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
const waterVolume = a => { | |
const max = Math.max(...a) | |
let sum = 0 | |
for (let i = max; i >0; i--) { | |
let started = false, tmpSum = 0 | |
for (let j = 0; j < a.length; j++) { | |
if (a[j] >= i) { | |
if (!started) { | |
started = true | |
} else { | |
sum += tmpSum | |
tmpSum = 0 | |
} | |
} else if (started) { | |
tmpSum++ | |
} | |
} | |
} | |
return sum | |
} | |
console.log(waterVolume([1, 2, 1, 2, 4, 3, 1, 3, 4, 5, 6, 2, 1, 2, 3, 1])) // 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment