Skip to content

Instantly share code, notes, and snippets.

@c-j-j
Created November 21, 2018 09:27
Show Gist options
  • Save c-j-j/19547b8c8b07efe1f3c6fa01d40480a2 to your computer and use it in GitHub Desktop.
Save c-j-j/19547b8c8b07efe1f3c6fa01d40480a2 to your computer and use it in GitHub Desktop.
Counting Valleys - Javascript
// Complete the countingValleys function below.
function countingValleys(n, s) {
let level = 0;
let numberOfValleys = 0;
for (let i = 0; i < n; i++) {
const next = s[i]
if (next === 'U') {
if (level === -1) {
numberOfValleys = numberOfValleys + 1;
}
level = level + 1;
} else if (next === 'D') {
level = level - 1;
}
}
return numberOfValleys;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment