• + 0 comments
    int countingValleys(int steps, string path) {
        int path_counter = 0, valeys = 0;
        bool topo = true;
        
        for(char c : path){
            
            if(c=='U'){
                path_counter++;
                
                if(path_counter>=0){
                    topo = true;
                }
            }
            
            if(c=='D'){
                path_counter--;
                
                if(path_counter<0 && topo){
                    valeys++;
                    topo = false;
                }
                
            }
        }
        
        return valeys;
    }