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.
Lena is ultimately trying to maximize her luck. We have a list of contests along with their luck and whether they are important or not. In a perfect world she would just bank as much luck as possible and lose every contest, but there is a constraint that stops her from doing this. You can only lose k important contests. So I thought of k as special tokens that I can spend. So for each contest you have 3 scenarios:
If it's not important lose it and keep the luck.
If is it important and you have special (k > 0) left spend 1 token, lose it and keep the luck.
If it's important and you are out of tokens (k < 0) then you have to win it.
Lena wants to maximize her luck, so if she spends a special token, it better be worth it. In other words be greedy and only spend tokens on the contests that will get you the most luck. With this as a guide you can sort the contests by luck in descending order, then pick the contests that will get you the most luck while spending special tokens. Because the contests are sorted by luck, when you spend a special token (k-= 1) on a contest i, you can be sure that there is no other contest that you can spend a special token on, that will get you more luck or else it would occur before contest i in the array, meaning that you have the optimal solution.
Running Time:
Sorting the array: O(n log n)
scanning the array while spending special tokens: O(n)
Because O(n) is bounded above by O(n log n) the running time of this algorithm is O(n log n)
Python Solution:
defluckBalance(k,contests):# sort by luckcontests=sorted(contests,key=lambdacontest:contest[0],reverse=True)max_luck=0forcontestincontests:ifcontest[1]==0:max_luck+=contest[0]elifcontest[1]==1andk>0:max_luck+=contest[0]k-=1elifcontest[1]==1andk==0:max_luck-=contest[0]returnmax_luck
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 is my intuition for solving this problem:
Lena is ultimately trying to maximize her luck. We have a list of contests along with their luck and whether they are important or not. In a perfect world she would just bank as much luck as possible and lose every contest, but there is a constraint that stops her from doing this. You can only lose k important contests. So I thought of k as special tokens that I can spend. So for each contest you have 3 scenarios:
Lena wants to maximize her luck, so if she spends a special token, it better be worth it. In other words be greedy and only spend tokens on the contests that will get you the most luck. With this as a guide you can sort the contests by luck in descending order, then pick the contests that will get you the most luck while spending special tokens. Because the contests are sorted by luck, when you spend a special token (k-= 1) on a contest i, you can be sure that there is no other contest that you can spend a special token on, that will get you more luck or else it would occur before contest i in the array, meaning that you have the optimal solution.
Running Time:
Because O(n) is bounded above by O(n log n) the running time of this algorithm is O(n log n)
Python Solution: