본문 바로가기
HackerRank Algorithm

[HackerRank] 17. Counting Valleys

by KIha_Jung 2020. 4. 1.

Counting Valleys

def countingValleys(n, s):
    height = 0
    pre_height = 0
    result = 0
    
    for i in s:
        # 이전 높이 계산
        pre_height = height
        # 현재 높이 계산
        if i =='D':
            height -= 1
        else:
            height += 1
            
        if height == 0 and pre_height < 0:
            result += 1
    return result

def main():
    n = int(input())
    s = input()
    
    result = countingValleys(n, s)
    print(result)
    
if __name__ == '__main__':
    main()

댓글