Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Stefanacef/307d3e8e710cb015f274be321c08cc91 to your computer and use it in GitHub Desktop.
Save Stefanacef/307d3e8e710cb015f274be321c08cc91 to your computer and use it in GitHub Desktop.
Solution for "Counting Valleys" in Hackerrank
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