-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFish.java
More file actions
32 lines (29 loc) Β· 937 Bytes
/
Copy pathFish.java
File metadata and controls
32 lines (29 loc) Β· 937 Bytes
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
import java.util.Stack;
public class Fish {
public int solution(int[] A, int[] B) {
int remainingFishCount = 0;
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < A.length; i++) {
if (B[i] == 1) {
stack.push(A[i]);
} else {
if (stack.isEmpty()) {
remainingFishCount++;
} else {
while (!stack.isEmpty()) {
if (stack.peek() < A[i]) {
stack.pop();
if (stack.isEmpty()) {
remainingFishCount++;
break;
}
} else {
break;
}
}
}
}
}
return remainingFishCount + stack.size();
}
}