• + 0 comments
    def counting_valleys(steps, path):
        count = level = 0
        in_valley = False
        for i in range(steps):
            level += 1 if path[i] == "U" else -1
            if level == -1 and path[i] == "D" and not in_valley:
                count += 1
                in_valley = True
            if level == 0 and path[i] == "U" and in_valley:
                in_valley = False
        return count