Skip to content

Commit

Permalink
Merge pull request #69 from imraghav20/master
Browse files Browse the repository at this point in the history
added Rain Water Saving Interview Problem (FAANG)
  • Loading branch information
akshitagupta15june authored Sep 26, 2020
2 parents 1eb70ec + bc8f76d commit fa6484c
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Interview_questions/rain_water_saving.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
n = int(input())
elevation = list(map(int, input().split()))

maxFromLeft = [0 for i in range(n)]
maxFromRight = [0 for i in range(n)]

maxFromLeft[0] = elevation[0]
for i in range(1, n):
maxFromLeft[i] = max(maxFromLeft[i-1], elevation[i])

maxFromRight[n-1] = elevation[n-1]
for i in range(n-2, -1, -1):
maxFromRight[i] = max(maxFromRight[i+1], elevation[i])

ans = 0
for i in range(n):
ans += min(maxFromLeft[i], maxFromRight[i]) - elevation[i]

print(ans)

0 comments on commit fa6484c

Please sign in to comment.