We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
publicstaticvoidminimumBribes(List<Integer>q){intn=q.size();intbribes=0;for(inti=n;i>=1;i--){// quick exit if number is in the right placeif(q.get(i-1)==i){continue;}// case where expected element is only one// places aheadif(q.get(i-2)==i){bribes++;Collections.swap(q,i-1,i-2);}// case where expected element has moved two// places awayif(q.get(i-3)==(i)){bribes+=2;Collections.rotate(q.subList(i-3,i),-1);}// case where expected element has moved ahead// further placeselse{System.out.println("Too chaotic");return;}}System.out.printf("%d\n",bribes);}}System.out.printf("%d\n",bribes);}
Cookie support is required to access HackerRank
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 →
This is the solution for Java