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.
Some performance add-ons would be to just add the zero luck values directly to the resultant luck. The new array would only contain the must win.
Sort the array(O(k * log(k)) where k is the number of 1s).
Finally, Iterate the 1s array and where for each n:
if(k <= 0) => result = result - n;
else => result = result + n; k--;
Basically, accumulate all the luck you can until you run out of k, once k is zero, start reducing your luck (you will be spending least luck value for reduction)
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 →
Some performance add-ons would be to just add the zero luck values directly to the resultant luck. The new array would only contain the must win.
Sort the array(O(k * log(k)) where k is the number of 1s).
Finally, Iterate the 1s array and where for each n: if(k <= 0) => result = result - n; else => result = result + n; k--;
Basically, accumulate all the luck you can until you run out of k, once k is zero, start reducing your luck (you will be spending least luck value for reduction)