[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

좋은 웹페이지 즐겨찾기