HackerRank: Counting Valleys
Tags: hackerrank, 紮馬步
Counting Valleys
- 真心覺得要練佛~ 靜下來 XDDD 好好思考!!!
-
- 我用最熟悉的方法,手寫 flow chart 來幫助思考
-
-
- python
# Complete the countingValleys function below.
def countingValleys(n, s):
valley_deep = 0
valley_count = 0
# s: seal level, m: mountain, v: valley
current_position = 's'
for step in s:
if step == 'D':
valley_deep -= 1
if valley_deep == 0:
current_position = 's'
if valley_deep > 0:
current_position = 'm'
if current_position != 'v' and valley_deep < 0:
current_position = 'v'
valley_count += 1
elif step == 'U':
valley_deep += 1
if valley_deep == 0:
current_position = 's'
if valley_deep > 0:
current_position = 'm'
if current_position != 'v' and valley_deep < 0:
current_position = 'v'
valley_count += 1
return valley_count