• + 0 comments

    def countingValleys(steps, path):

    valley_count = 0  
    altitude = 0  
    
    for step in path:
        if step == 'U':  
            altitude += 1
        else:  
            altitude -= 1
    
        if altitude == 0 and step == 'U':
            valley_count += 1
    
    return valley_count