package leetcode;

/**
 * https://leetcode.com/problems/validate-stack-sequences/
 */
public class Problem946 {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        // TODO
        return false;
    }

    public static void main(String[] args) {
        Problem946 prob = new Problem946();
        System.out.println(prob.validateStackSequences(
            new int[]{1, 2, 3, 4, 5}, new int[]{4, 5, 3, 2, 1}
        )); // true
        System.out.println(prob.validateStackSequences(
            new int[]{1, 2, 3, 4, 5}, new int[]{4, 3, 5, 1, 2}
        )); // false
    }
}

@@ -12,16 +12,41 @@
 tcode;%0A%0A
+import java.util.Stack;%0A%0A
 /**%0A * h
@@ -214,16 +214,60 @@
     
-// TODO%0A
+if (pushed.length == 0 %7C%7C popped.length == 0) %7B%0A    
     
@@ -281,389 +281,649 @@
 urn 
-fals
+tru
 e;%0A    
-%7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem946 prob = new Problem946();%0A        System.out.println(prob.validateStackSequences(%0A            new int%5B%5D%7B1, 2, 3, 4, 5%7D, new int%5B%5D%7B4, 5, 3, 2, 1%7D%0A        )); // true%0A        System.out.println(prob.validateStackSequences(%0A            new int%5B%5D%7B1, 2, 3, 4, 5%7D, new int%5B%5D%7B4, 3, 5, 1, 2%7D%0A        )); // false
+    %7D%0A        Stack%3CInteger%3E stack = new Stack%3C%3E();%0A        int pushIdx = 0;%0A        int popIdx = 0;%0A        stack.push(pushed%5BpushIdx++%5D);%0A        while (pushIdx %3C pushed.length && popIdx %3C popped.length) %7B%0A            if (!stack.isEmpty() && stack.peek() == popped%5BpopIdx%5D) %7B%0A                popIdx++;%0A                stack.pop();%0A            %7D else %7B%0A                stack.push(pushed%5BpushIdx++%5D);%0A            %7D%0A        %7D%0A        while (!stack.isEmpty()) %7B%0A            if (stack.peek() != popped%5BpopIdx%5D) %7B%0A                return false;%0A            %7D%0A            popIdx++;%0A            stack.pop();%0A        %7D%0A        return true;
 %0A   

Solve problem 946