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()
'HackerRank Algorithm' 카테고리의 다른 글
[HackerRank] 19. Bon Appétit (0) | 2020.04.01 |
---|---|
[HackerRank] 18. Breaking the Records (0) | 2020.04.01 |
[HackerRank] 16. Day of the programmer (0) | 2020.04.01 |
[HackerRank] 15. Diagonal Difference (0) | 2020.03.09 |
[HackerRank] 14. Divisible Sum pairs (0) | 2020.03.09 |
댓글