[HackerRank] Counting Valleys

Jongmin Lee (SAVZAK)·2021년 6월 8일
0

HackerRank

목록 보기
11/39

[문제 링크]

[입력]

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
profile
느리지만 단단하게 걷는 개발자

0개의 댓글