Trains the technique from
LeetCode 2600K Items With the Maximum SumThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A tin holds numOnes tokens marked 1, numZeros marked 0 and numNegOnes marked -1.
Take exactly k tokens out of the tin. Return the largest total the marks on those tokens can come to.
Example 1
Taking all seven ones and all four zeros accounts for eleven tokens, so the last two must be negatives, giving 7 less 2.
Example 2
All three ones fit and the remaining two tokens are zeros, so nothing pulls the total down.
Example 3
The tin holds nothing but negatives and all nine must come out.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def k_items_with_maximum_sum(numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:public int kItemsWithMaximumSum(int numOnes, int numZeros, int numNegOnes, int k)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.