-
Notifications
You must be signed in to change notification settings - Fork 15
/
Fish.cs
34 lines (31 loc) · 937 Bytes
/
Fish.cs
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
public static int solution(int[] A, int[] B)
{
Stack<int> upstreamQueue = new Stack<int>(); // 0's
Stack<int> downstreamQueue = new Stack<int>(); // 1's
for (int i = 0; i < A.Length; i++)
{
if (B[i] == 0) // is upstream
{
if (downstreamQueue.Count > 0) // there is fish to eat
{
while (downstreamQueue.Count > 0 && downstreamQueue.Peek() < A[i])
{
downstreamQueue.Pop();
}
if (downstreamQueue.Count == 0)
{
upstreamQueue.Push(A[i]);
}
}
else
{
upstreamQueue.Push(A[i]);
}
}
else // is downstream
{
downstreamQueue.Push(A[i]);
}
}
return upstreamQueue.Count + downstreamQueue.Count;
}