Created
October 16, 2021 10:51
-
-
Save Stefanacef/307d3e8e710cb015f274be321c08cc91 to your computer and use it in GitHub Desktop.
Solution for "Counting Valleys" in Hackerrank
This file contains 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
let path = [ | |
'DDUDUDDUDDUDDUUUUDUDDDUUDDUUDDDUUDDUUUUUUDUDDDDUDDUUDUUDUDUUUDUUUUUDDUDDDDUDDUDDDDUUUUDUUDUUDUUDUDDD'] | |
const cat = [1, 2, 3]; | |
const steps = 8; | |
function countingValleys(steps, path) { | |
let count = 0; | |
let val = 0; | |
let distance = steps; | |
const arr = path[0] | |
.split("") | |
.map((el) => | |
el === "U" ? count++ && (count === 0 ? val++ : val) : count-- | |
); | |
////////////OR | |
// for (let i = 0; i < arr.length; i++) { | |
// if (arr[i] === "U") { | |
// count++; | |
// if (count === 0) { | |
// val++; | |
// } | |
// } else { | |
// count--; | |
// } | |
// } | |
return val; | |
} | |
console.log(countingValleys(steps, path)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment