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){// Write your code hereintnumBribes=0;booleanchaotic=false;intn=q.size();Map<Integer,Boolean>found=newHashMap<>();found.put(0,true);intj=0;for(inti=0;i<n;i++){booleanbribeGiven=false;//get eleminte=q.get(i);//did they bribeintexpected=i+1;intdiff=e-expected;if(diff>2){chaotic=true;break;}while(j<e){if(!found.containsKey(j)){//not in their positionbribeGiven=true;if(e==expected||e<expected){diff=1;}break;}j++;}if(bribeGiven){numBribes+=Math.abs(diff);}found.put(e,true);}System.out.println((chaotic)?"Too chaotic":numBribes);}}
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 →