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.

commented Java solution :) it must be pretty optimal

// Idea: 1. find the sum of all luck values; 2. save the luck values of all the important contests in a list and sort it; 3. find the (number of contests to be won) minimum values of the list and subtract them twice from the sum (since they were already added once); 4. return the sumstaticintluckBalance(intk,int[][]contests){intsum=0;introws=contests.length;ArrayList<Integer>sort=newArrayList<>();// find the sum of all the luck values; add all the luck values of the important contests to the ArrayList sortfor(inti=0;i<rows;i++){sum+=contests[i][0];if(contests[i][1]==1){sort.add(contests[i][0]);}}// sort the ArrayList sortCollections.sort(sort);// the number of contests to be won = num important - num to be lost// we subtract 1 from this number, since we use numLostImportant as an index in the while-loopintnumLostImportant=sort.size()-k-1;// since sort is already sorted, we subtract the first (and lowest) sort.size() - k values TWICE (firstly, to subtract them from the sum; secondly, to subtract them since they will be won)while(numLostImportant>-1){sum-=2*sort.get(numLostImportant);numLostImportant--;}returnsum;}

## Luck Balance

You are viewing a single comment's thread. Return to all comments →

commented Java solution :) it must be pretty optimal