Skip to content

Instantly share code, notes, and snippets.

@adleroliveira
Created August 29, 2018 00:45
Show Gist options
  • Save adleroliveira/acc88444107f7fbb731a612f3060c8e7 to your computer and use it in GitHub Desktop.
Save adleroliveira/acc88444107f7fbb731a612f3060c8e7 to your computer and use it in GitHub Desktop.
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