We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 1eb70ec + bc8f76d commit fa6484cCopy full SHA for fa6484c
Interview_questions/rain_water_saving.py
@@ -0,0 +1,19 @@
1
+n = int(input())
2
+elevation = list(map(int, input().split()))
3
+
4
+maxFromLeft = [0 for i in range(n)]
5
+maxFromRight = [0 for i in range(n)]
6
7
+maxFromLeft[0] = elevation[0]
8
+for i in range(1, n):
9
+ maxFromLeft[i] = max(maxFromLeft[i-1], elevation[i])
10
11
+maxFromRight[n-1] = elevation[n-1]
12
+for i in range(n-2, -1, -1):
13
+ maxFromRight[i] = max(maxFromRight[i+1], elevation[i])
14
15
+ans = 0
16
+for i in range(n):
17
+ ans += min(maxFromLeft[i], maxFromRight[i]) - elevation[i]
18
19
+print(ans)
0 commit comments