You are viewing a single comment's thread. Return to all comments →
public static void minimumBribes(List q) { // Write your code here LinkedList maineQ = new LinkedList<>(q);
LinkedList<Integer> dQ = new LinkedList<>(q); Collections.sort(dQ); boolean isChaotic=false; // System.out.println(maineQ); //System.out.println(dQ); int sum=0; while(!maineQ.isEmpty()){ int qInt = maineQ.peek(); //System.out.println("queue "+qInt); //System.out.println("list "+dQ.peek()); if(qInt!=dQ.peek()){ int indexDq=dQ.indexOf(qInt); int indexMq=maineQ.indexOf(qInt); //System.out.println("index dq"+indexDq); //System.out.println("index mq"+indexMq); if(Math.abs(indexDq-indexMq)>2){ isChaotic=true; break; }else{ sum=sum+Math.abs(indexDq-indexMq); // System.out.println("bribe "+sum); } maineQ.pop(); dQ.remove(indexDq); }else{ maineQ.pop(); dQ.pop(); } //System.out.println(maineQ); //System.out.println(dQ); isChaotic=false; } if(isChaotic){ System.out.println("Too chaotic"); }else{ System.out.println(sum); } }
}
Seems like cookies are disabled on this browser, please enable them to open this website
New Year Chaos
You are viewing a single comment's thread. Return to all comments →
public static void minimumBribes(List q) { // Write your code here LinkedList maineQ = new LinkedList<>(q);
}