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){// Record how many numbers before the current element are larger than the element itself. The count will be stored in the check array at the corresponding index.//For example, given the array [1, 2, 4, 5, 3]://For the element 3 (at index 4), there are two elements (4 and 5) that are larger and appear before it.//Therefore, check[4] = 2.int[]check=newint[q.size()+1];Arrays.fill(check,0);intres=0;for(inti=0;i<q.size();i++){intn=q.get(i);for(intj=1;j<n;j++){check[j]++;}intori=n-1;intt=ori-i;if(t>2){res=-1;break;}if(t<=0){t=check[n]+t;}res+=t;}System.out.println(res>0?res:"Too chaotic");}
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 →
Java (Not a Sort Solution)