less than 1 minute read

Tags: ,

Counting Valleys

  • 真心覺得要練佛~ 靜下來 XDDD 好好思考!!!
    • Imgur
    • 我用最熟悉的方法,手寫 flow chart 來幫助思考 :heart:
    • Imgur
  • 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