-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathwaterfall-streams.py
48 lines (40 loc) · 1.39 KB
/
waterfall-streams.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
38
39
40
41
42
43
44
45
46
47
48
# WATERFALL STREAMS
# O(W^2*H) time, O(W) space -> W: Width, H: Height
def waterfallStreams(array, source):
# Write your code here.
height, width = len(array), len(array[0])
previousRow = [0 for _ in range(width)]
nextRow = [0 for _ in range(width)]
currentRowNumber = 0
previousRow[source] = 100
while currentRowNumber < height - 1:
nextRow = [0 for _ in range(width)]
# getting positions where water is present and will fall down
for i in range(width):
if array[currentRowNumber][i] == 0 and previousRow[i] > 0:
# for each position, checking if water will split or go down
position = i
# no block so goes down
if array[currentRowNumber + 1][position] == 0:
nextRow[position] += previousRow[position]
continue
amountOfWater = previousRow[position] / 2
current = position
while current > 0:
isMovePossible = array[currentRowNumber + 1][current] == 1 and array[currentRowNumber][current - 1] != 1
if isMovePossible:
nextRow[current - 1] += amountOfWater
else:
break
current -= 1
current = position
while current < width - 1:
isMovePossible = array[currentRowNumber + 1][current] == 1 and array[currentRowNumber][current + 1] != 1
if isMovePossible:
nextRow[current + 1] += amountOfWater
else:
break
current += 1
previousRow = nextRow
currentRowNumber += 1
return nextRow