CLooker

hackerRank counting valleys

Jan 29th, 2018
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // https://www.hackerrank.com/challenges/counting-valleys/problem
  2.  
  3. const countingValleys = (n, s) => (
  4.   s.split("")
  5.   .reduce(({ pos, vSteps, valleys }, step) => {
  6.     step === 'U' ? ++pos : --pos;
  7.     (pos < 0) && ++vSteps;
  8.     (pos === 0) && (vSteps > 0) && (++valleys) && (vSteps = 0);
  9.     return {pos, vSteps, valleys};
  10.   }, {pos: 0, vSteps: 0, valleys: 0}).valleys
  11. )
Add Comment
Please, Sign In to add comment