forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rainwater_harvesting.py
37 lines (30 loc) · 995 Bytes
/
rainwater_harvesting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution(object):
def trap(self, heights):
if not heights or len(heights) < 3:
return 0
level = water = 0
i = 0
j = len(heights) - 1
while i < j:
it = heights[i if heights[i] < heights[j] else j]
if heights[i] < heights[j]:
i += 1
else:
j -= 1
if level > it:
water += level - it
else:
level = it
return water
sol = Solution()
N = int(input("Enter Number of elements: "))
ilist = list(map(int, input("Enter non-negative numbers: ").split()))
print("Maximum units of water saved is " + str(sol.trap(ilist)))
"""
Time Complexity: O(n2)
Space Complexity: O(1)
Sample Input:
Enter Number of elements: 10
Enter non-negative element: 0 2 1 3 0 1 2 1 2 1
Output: Maximum units of water saved is 5
"""