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.
same idea, but your solution is much clearer and more concise
defluck_balance(k,contests):# group important and unimportant contestssorted_contests=sorted(contests,key=itemgetter(1))groups=tuple(list(g)fork,ginit.groupby(sorted_contests,key=itemgetter(1)))iflen(groups)>1:unimportant,important=groupselifgroups[0][0][1]==0:unimportant,important=groups[0],[]else:unimportant,important=[],groups[0]# calculate how many matches we need to throwthrows=len(important)-kifthrows<1:returnsum(luckforluck,_inimportant+unimportant)# start by sorting important contests to throw the smallest in terms of lucksorted_important=sorted(important)# group wins and losseslosses,wins=sorted_important[:throws],sorted_important[throws:]# do mathtotal=sum(luckforluck,_inwins+unimportant)net=total-sum(luckforluck,_inlosses)returnnet
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 →
same idea, but your solution is much clearer and more concise