[HackerRank] Counting Valleys


[问题链接]


[入力]


int steps: the number of steps on the hike
string path: a string describing the path

[出力]


int: the number of valleys traversed

[コード]

def countingValleys(steps, path):
    # Write your code here
    result = 0
    pos = 0
    for i in range(steps):
        if path[i] == 'U':
            pos +=1
        else:
            if pos==0:
                result +=1
            pos -=1
    return result