• + 40 comments

    Python 3 solution

    Just counting how many times I hit the sea level climbing up.

    #!/bin/python3
    
    import sys
    
    def countingValleys(n, steps):
        seaLevel = valley = 0
    
        for step in steps:
            if step == 'U':
                seaLevel += 1
            else:
                seaLevel -= 1
            
            if step == 'U' and seaLevel == 0:
                valley += 1
        
        return valley