Hackerrank: Counting Valleys Solution
1function countingValleys(steps, path) {2 let res = 03 let curr = 04 const splitedPath = path.split("")5 const mapped = [0, ...splitedPath.map(p => (curr += { U: 1, D: -1 }[p]))]6 let i = 07 while (i < steps) {8 if (mapped[i] === 0) {9 while (mapped[++i] !== 0 && i < steps) {}10 if (mapped[i - 1] < 0) res++11 }12 }13 return res14}
Comments
Loading comments...
Tags
hackerrank
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
Codility: TapeEquilibrium
Lesson 3 Complexity