Counting Valleys

  • + 0 comments
    function countingValleys(steps, path) {
        let i,temp=0,count=0;
        for(i=0; i<steps; i++){
            if(path[i]==="D"){
                temp--;
                
            }else{
                if(temp==-1){
                    count++;
                }
                temp++;
            }
        }
        
        return count;
    }