• + 0 comments

    Briefest I can make it in Python

    def countingValleys(steps, path):
        elevation = count = 0
        for i in range(steps):
            elevation += 1 if path[i] == "U" else -1
            if elevation == 0 and path[i] == "U":
                count += 1
        return count