I have passed all the test cases on this thread but still getting the WA.
Whats wrong with my formatting?? >.<
Code: Select all
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
LinkedList<Integer> original;
LinkedList<Integer> permutation;
Stack<Integer> stack;
int noOfCoaches = sc.nextInt();
boolean match;
while(noOfCoaches != 0)
{
while(true)
{
original = new LinkedList<Integer>();
permutation = new LinkedList<Integer>();
stack = new Stack<Integer>();
match = true;
int num = sc.nextInt();
if(num == 0)
{
System.out.printf("\n");
break;
}
permutation.offer(num);
for(int i = 0; i < noOfCoaches; i++)
{
original.offer(i+1);
}
for(int i = 1; i < noOfCoaches; i++)
{
permutation.offer(sc.nextInt());
}
while(original.size() > 0 && match)
{
stack.push(original.poll());
while(stack.peek() != permutation.peek() && original.size() > 0)
{
stack.push(original.poll());
}
while(!stack.empty() && match)
{
if(stack.peek() == permutation.peek())
{
stack.pop();
permutation.poll();
}
else if(original.peek() != null)
{
break;
}
else
{
match = false;
}
}
}
if(match) System.out.printf("Yes\n");
else System.out.printf("No\n");
}
noOfCoaches = sc.nextInt();
}
}
}
Code: Select all
5
1 2 3 4 5
5 4 1 2 3
0
6
6 5 4 3 2 1
5 6 4 3 2 1
1 2 3 4 5 6
0
10
1 2 3 4 5 10 9 8 7 6
0
0
Code: Select all
Yes
No
Yes
Yes
Yes
Yes