counting valleys
\(O(n)\) solution in Python to the counting valleys problem from HackerRank:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def countingValleys(steps, path):
level = 0
valleys = 0
for i in range(steps):
if path[i] == 'U':
level += 1
else:
level -= 1
if path[i] == 'U' and level == 0:
valleys += 1
return valleys
if __name__ == '__main__':
steps = 8
path = 'UDDDUDUU'
print(countingValleys(steps, path)) # 1