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.
My Java solution with o(n log n) time and o(n) space:
publicstaticintluckBalance(intk,List<List<Integer>>contests){// determine total luck balance // get list of important contests that holds each luck valintluckBalance=0;List<Integer>importantContests=newArrayList<>();// add the uninmportant contests to the luck valfor(inti=0;i<contests.size();i++){intluckVal=contests.get(i).get(0);intimportant=contests.get(i).get(1);if(important==1)importantContests.add(luckVal);elseluckBalance+=luckVal;}//sort descending to get highest competitions added firstCollections.sort(importantContests,Collections.reverseOrder());//for each important contest add the important valsfor(intj=0;j<importantContests.size();j++){luckBalance+=(j<k)?importantContests.get(j):-importantContests.get(j);}returnluckBalance;}
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 →
My Java solution with o(n log n) time and o(n) space: