package com.johnny.java;

import java.util.Stack;

/**
 * Created by Johnny on 2/16/2017.
 */
public class Fish {
    public static void main(String[] args) {
//        int[] a = {4, 3, 2, 1, 5, 9, 8, 11, 10, 6, 7};
//        int[] b = {1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0};
        int[] a = {4, 3, 2, 1, 5};
        int[] b = {0, 1, 0, 0, 0};

        Fish f = new Fish();
        System.out.println(f.solution(a, b));
        System.out.println(f.solutionB(a, b));
    }

    public int solution(int[] A, int[] B) {
        Stack<int[]> stack = new Stack<>();
        for (int i = 0; i < A.length; i++) {
            int[] temp = new int[2];
            temp[0] = A[i];
            temp[1] = B[i];
            if (stack.isEmpty()) {
                stack.push(temp);
                continue;
            }
            if (B[i] == 1) {
                stack.push(temp);

            } else if (stack.peek()[1] != B[i]) {
                while (!stack.isEmpty() &&
                        stack.peek()[0] < A[i] &&
                        stack.peek()[1] != B[i]) {
                    stack.pop();
                }
                if (!stack.isEmpty()
                        && stack.peek()[0] > A[i]
                        && stack.peek()[1] != B[i]) {
                    continue;
                } else {
                    stack.push(temp);
                }
            } else if (stack.peek()[1] == B[i]) {
                stack.push(temp);
            }
        }
        return stack.size();
    }


    public int solutionB(int[] A, int[] B) {
        Stack<Integer> stack = new Stack<>();
        int count = 0;
        for (int i = 0; i < B.length; i++) {
            if (B[i] == 1) {
                stack.push(A[i]);
            } else {
                count++;
                while (stack.size() > 0) {
                    int item = stack.pop();
                    if (A[i] < item) {
                        stack.push(item);
                        count--;
                        break;
                    }
                }
            }
        }
        return count + stack.size();
    }
}

@@ -152,18 +152,16 @@
 args) %7B%0A
-//
         
@@ -211,10 +211,8 @@
 7%7D;%0A
-//
     
@@ -260,16 +260,18 @@
  1, 0%7D;%0A
+//
         
@@ -297,16 +297,18 @@
  1, 5%7D;%0A
+//
         
@@ -1100,32 +1100,250 @@
               %7D%0A
+                if (stack.isEmpty()%0A                        %7C%7C (stack.peek()%5B0%5D %3C= A%5Bi%5D%0A                        %7C%7C stack.peek()%5B1%5D == B%5Bi%5D)) %7B%0A                    stack.push(temp);%0A                %7D%0A                /*%0A
                 
@@ -1570,32 +1570,51 @@
               %7D%0A
+                */%0A
             %7D el

fix question of Fish