See the original problem on HackerRank.
Solutions
Simple observation: there is no point in winning unimportant contests so we can lose them all to get their luck.
If \(K\) is greater than the number of important contests, we can lose them all. In this case the luck balance is just the sum of all the contest values.
Otherwise, we have to win some important contests but we don’t have to lose more than \(K\). So the number of important contests to win is:
\(W = N_{important} - K\).
Since we need to maximize the luck, we’d better off winning the contests with the smallest luck value (because their luck will be subtracted from the total).
Thus, the problem boils down to finding \(W\) smallest luck values in an array.
A simple approach (that works) consists in sorting the important contests and then picking up the first \(W\) elements.
Here is a C++ solution implementing this idea:
|
|
Notes:
- we eagerly accumulate
luck
along the way by summing up all the luck values lostLuck
must be doubled because we have previously added its contribution toluck
In addition to this, consider that the solution schema hides some interesting patterns we can express, for example, in C# with LINQ:
|
|
|
|
This problem open doors to investigating better and more generic ways to finding the first \(n\) smallest/biggest values of a sequence.
Actually, not only sorting the sequence is more expensive but also unnecessary. Indeed, the actual order of the first \(W\) elements is not important for our problem because addition is a commutative operation.
Another option is using a partial sort algorithm, however the overall complexity would be similar.
The game changer is a selection algorithm like Quickselect that can solve this problem in linear time. The C++ standard library provides nth_element
that typically implements introselect or other selection algorithms with suitable average-case complexity (linear).
Here is the affected code fragment:
|
|