Codility - Fish

RESULT

import java.util.*;

class Solution {
    public int solution(int[] A, int[] B) {
        Stack<Integer> upStack = new Stack<>();
        Stack<Integer> downStack = new Stack<>();
        
        for (int i=0; i<A.length; i++) {
            if (B[i] == 1) {
                downStack.push(A[i]);
            } else if (!downStack.isEmpty()) {
                while (!downStack.isEmpty() && downStack.peek() < A[i]) {
                    downStack.pop();
                }
                if (downStack.isEmpty()) {
                    upStack.push(A[i]);
                }
                
            } else {
                upStack.push(A[i]);
            }
        }
    
        return upStack.size() + downStack.size();    
    }
}