Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions interview-preparation-kit/counting-valleys.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,14 @@
import sys

def countingValleys(n, s):
res = 0
in_valley = 0
curr = 0

for step in s:
if step == 'U':
curr += 1
else:
curr -= 1

if curr < 0 and in_valley == 0:
in_valley = 1
if in_valley == 1 and curr == 0:
in_valley = 0
res += 1

return res
stepSum=0
valleyCount=0
for i in path:
stepSum=stepSum+1 if i=="U" else stepSum
stepSum=stepSum-1 if i=="D" else stepSum
if stepSum==0 and i=="U":
valleyCount=valleyCount+1
return (valleyCount)

if __name__ == "__main__":
n = int(input().strip())
Expand Down