-
Notifications
You must be signed in to change notification settings - Fork 56
/
Fish.py
33 lines (23 loc) · 847 Bytes
/
Fish.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
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A, B):
# write your code in Python 3.6
downstream_stack = []
n = len(A)
num_alive = n
for i in range(n):
# downstream
if B[i] == 1:
downstream_stack.append( A[i] )
# upstream
if B[i] == 0:
while len(downstream_stack) != 0:
pop = downstream_stack.pop( len(downstream_stack)-1 )
if pop >= A[i]:
num_alive -=1
downstream_stack.append(pop) # just push back
break
elif pop < A[i]:
num_alive -=1
# keep looping (pop another downstrem fish)
return num_alive