forked from MTrajK/coding-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrapped_watter.py
More file actions
74 lines (54 loc) · 2.1 KB
/
trapped_watter.py
File metadata and controls
74 lines (54 loc) · 2.1 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'''
Trapped Water
You are given an array of non-negative integers that represents a two-dimensional elevation map where each element is unit-width wall and the integer is the height.
Suppose it will rain and all spots between two walls get filled up.
Compute how many units of water remain trapped on the map in O(N) time and O(1) space.
Input: [2, 1, 2]
Output: 1
Output explanation: We can hold 1 unit of water in the middle.
Input: [3, 0, 1, 3, 0, 5]
Output: 8
Output explanation: We can hold 3 units in the first index, 2 in the second, and 3 in the fourth index (we cannot hold 5 since it would run off to the left), so we can trap 8 units of water.
=========================================
The goal is to find the max wall and make 2 iterations starting from front and from back looking for the next bigger wall.
First search for the max wall from front, after that correct the left water starting from the back side
Time Complexity: O(N)
Space Complexity: O(1)
'''
############
# Solution #
############
def trapped_water(elevation_map):
n = len(elevation_map)
if n == 0:
return 0
water = 0
# start from front of the array
# and look for the max wall
max_idx = 0
max_height = elevation_map[0]
for i in range(1, n):
if elevation_map[i] >= max_height:
max_idx = i # save the highest wall index for later
max_height = elevation_map[i]
water += max_height - elevation_map[i]
# after that start from back and go reverse to the max wall idx
# and correct the result (pour the extra water if there is smaller wall on the right side)
back_max_height = elevation_map[-1]
for i in range(n - 1, max_idx, -1):
if elevation_map[i] > back_max_height:
back_max_height = elevation_map[i]
water -= max_height - back_max_height
return water
###########
# Testing #
###########
# Test 1
# Correct result => 1
print(trapped_water([2, 1, 2]))
# Test 2
# Correct result => 8
print(trapped_water([3, 0, 1, 3, 0, 5]))
# Test 3
# Correct result => 6
print(trapped_water([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]))