-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFish.java
More file actions
32 lines (29 loc) · 994 Bytes
/
Fish.java
File metadata and controls
32 lines (29 loc) · 994 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
//https://app.codility.com/demo/results/trainingNHPEBU-FYS/
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A, int[] B) {
// write your code in Java SE 8
Stack<Integer> s = new <Integer>Stack(); //storage the downstream fish
int alive = A.length;//count hw many fish it is eaten
if (alive ==0) return 0;
for(int i=0;i<A.length;i++){
if(B[i] == 1){
s.push(new Integer(A[i]));
}else {
while ((!s.isEmpty())){
if(s.peek() > A[i]){
alive--;
break;
}
if(s.peek() < A[i]){
s.pop();
alive--; //another downstream fish can eat now
}
}
}
}
return alive;
}
}