content: import java.util.Stack;
  
  /**
   * @author jzy
   * @GitHub <a href="https://github.com/567jin">567jin</a>
   * @since 2024-04-25 9:04:31
   */
  public class _946_ValidateStackSequences {
      public static void main(String[] args) {
          int[] pushed = new int[]{1, 2, 3, 4, 5};
          int[] popped = new int[]{4, 5, 3, 2, 1};
          boolean b = new _946_ValidateStackSequences().validateStackSequences(pushed, popped);
          System.out.println(b);
      }
  
      public boolean validateStackSequences(int[] pushed, int[] popped) {
          Stack<Integer> stack = new Stack<>();
          int i = 0;
          for (int num : pushed) {
              stack.push(num);
              while (!stack.isEmpty() && stack.peek() == popped[i]) {
                  stack.pop();
                  i++;
              }
          }
          return stack.isEmpty();
      }
  
      public boolean validateStackSequences2(int[] pushed, int[] popped) {
          int i = 0, j = 0;
          for (int num : pushed) {
              pushed[i++] = num;
              while (i > 0 && pushed[i - 1] == popped[j]) {
                  i--;
                  j++;
              }
          }
          return i == 0;
      }
  
  }
  