📌  相关文章
📜  数谷hackerrank解决方案python代码示例

📅  最后修改于: 2022-03-11 14:46:36.054000             🧑  作者: Mango

代码示例1
# Don't copy it. Of course you wouldn't right?
def countingValleys(steps, path):
   # if value of current_level = 0 they're currently in sea level 
   # and negative means below sea level
  # positive means above sea level
    current_level = 0  
  # number of times they scape the valley is the number of valley
  # there is
    no_of_valleys = 0
    
    for position in path:
        if position == "D":   
            current_level -= 1
        elif position == "U":
            current_level += 1
            # when they are going up and finally reach sea level,
            # i.e current_level=0, they finally escape a valley
            if current_level == 0:
                no_of_valleys += 1
        
    return no_of_valleys