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.
Here's my solution in Java 8, which passes all test cases.
The basic idea is to store the List<List<Integer>> into a List<ContestParams>. And then sort that in descending order, based first on impRating, and for the same impRating values, further sort based on descending order of luckBal. Once the list is sorted, then all simply iterate sequentially, and keep losing the first k "important contests", following which, all other "important contests" are won. And of course, lose all "non-important contests". And keep adding / subtracting the individual luckBal from the maxLuckBal total, to get the final result.
staticclassContestParams{IntegerluckBal;BooleanimpRating;publicContestParams(IntegerluckBal,BooleanimpRating){this.luckBal=luckBal;this.impRating=impRating;}publicIntegergetLuckBal(){returnluckBal;}publicBooleangetImpRating(){returnimpRating;}@OverridepublicStringtoString(){return"{"+luckBal+", "+impRating+"}";}}/* * Complete the 'luckBalance' function below. * * The function is expected to return an INTEGER. * The function accepts following parameters: * 1. INTEGER k * 2. 2D_INTEGER_ARRAY contests */publicstaticintluckBalance(intk,List<List<Integer>>contests){// Write your code hereintmaxLuckBal=0;List<ContestParams>contestsParamsLst=newArrayList<>();for(intcIdx=0;cIdx<contests.size();cIdx++){IntegerluckBal=contests.get(cIdx).get(0);BooleanimpRating=contests.get(cIdx).get(1)!=0?Boolean.TRUE:Boolean.FALSE;contestsParamsLst.add(cIdx,newContestParams(luckBal,impRating));}contestsParamsLst.sort(Comparator.comparing(ContestParams::getImpRating).reversed().thenComparing(ContestParams::getLuckBal,Comparator.reverseOrder()));intcurNumImpContestsLost=0;for(intcIdx=0;cIdx<contestsParamsLst.size();cIdx++){ContestParamscurContestParams=contestsParamsLst.get(cIdx);if(!curContestParams.getImpRating()||curNumImpContestsLost<k){maxLuckBal+=curContestParams.getLuckBal();if(curContestParams.getImpRating()){curNumImpContestsLost++;}}else{maxLuckBal-=curContestParams.getLuckBal();}}returnmaxLuckBal;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Luck Balance
You are viewing a single comment's thread. Return to all comments →
Here's my solution in Java 8, which passes all test cases.
The basic idea is to store the
List<List<Integer>>
into aList<ContestParams>
. And then sort that in descending order, based first onimpRating
, and for the sameimpRating
values, further sort based on descending order ofluckBal
. Once the list is sorted, then all simply iterate sequentially, and keep losing the firstk
"important contests", following which, all other "important contests" are won. And of course, lose all "non-important contests". And keep adding / subtracting the individualluckBal
from themaxLuckBal
total, to get the final result.