Skip to content

Instantly share code, notes, and snippets.

@charliewilco
Created July 29, 2019 04:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charliewilco/8caf5dfbd3250bd86377e296c4e5417c to your computer and use it in GitHub Desktop.
Save charliewilco/8caf5dfbd3250bd86377e296c4e5417c to your computer and use it in GitHub Desktop.
function countingValleys(n, s) {
let seaLevel = 0;
let currLevel = 0;
let valleys = 0;
s = s.split('');
for (let i = 0; i < s.length; i++) {
//update the current level
if (s[i] === 'U') {
currLevel += 1;
if (currLevel == 0) {
valleys += 1;
}
} else {
currLevel -= 1;
}
}
return valleys;
}
function jumpingOnClouds(c) {
let jumps = 0
let i = 0
while ( i < c.length) {
if (c[i+2] === 0) i += 2
else i += 1
if (i !== c.length) jumps++
// This one makes you really understand what is happening
console.log({jumps, i})
}
return jumps
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment