• + 0 comments

    My solution c++

    int countingValleys(int steps, string path) {
     int stepsCount=0,valley=0;
     for(int i=0; i<steps; i++){
        if(path[i]=='U'){
          stepsCount++;
          if(stepsCount==0) valley++;
        }
        else if(path[i]=='D'){
          stepsCount--;
        }
     }
     return valley;
    }