Split Chores Between Two Workers With a Fixed Quota
Greedy & IntervalsAuto-graded · 21 test casesLast updated
Choose exactly k items for the first worker to maximize total payoff, which tests reducing a problem to per-item differences and greedy top-k selection.
A manager has n chores to hand out between two workers, Alice and Bob. Every chore goes to exactly one of them.
You are given two integer lists of equal length n:
alice_pay[i]is the payoff if Alice does chorei.bob_pay[i]is the payoff if Bob does chorei.
Alice must receive exactly k chores, and Bob receives the other n - k. Any set of k chores may go to Alice; they do not need to be adjacent or in any particular order.
Return the largest possible total payoff over all valid ways to split the chores.
Payoffs may be negative. You must still give Alice exactly k chores, even if that lowers the total.
Constraints
0 <= n <= 10^5alice_payandbob_payboth have lengthn0 <= k <= n- Payoff values are integers (they may be negative, and are small enough that sums are exact in Python)
- The function should run efficiently for
nup to10^5
Examples
Example 1
Input: max_split_payoff([5, 2, 3], [1, 6, 2], 2)
Output: 14
Example 2
Input: max_split_payoff([1, 2, 3], [3, 2, 1], 1)
Output: 8
Example 3
Input: max_split_payoff([7, 4], [2, 9], 0)
Output: 11
Hints
Hint 1
Imagine first giving every chore to Bob. What is the total in that case, and how does it change if you move just one chore to Alice?
Hint 2
The change from moving chore i to Alice is alice_pay[i] - bob_pay[i], and it does not depend on any other chore. So the problem becomes choosing k of these differences with the largest sum.
Hint 3
Sort the differences in descending order and add the first k to Bob's baseline total. Do not skip negative differences, because Alice must receive exactly k chores.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Start from the assignment where Bob does every chore. Its total is base = sum(bob_pay).
Moving chore i from Bob to Alice changes the total by gain[i] = alice_pay[i] - bob_pay[i]. Each chore's change depends only on that chore, so for any set S of k chores given to Alice:
total = base + sum(gain[i] for i in S)
Since base is fixed, we just need the k chores with the largest gains. Compute all gains, sort them in descending order, and add the first k to base.
Why it is correct
- The reformulation is exact. Every valid assignment corresponds to a
k-element setS, and every such set is valid because chores need not be contiguous. The total forSisbase + sum of gains in S. - Top-k gains are optimal (exchange argument). Suppose an optimal set
S*differs from the top-ksetT. Then there areiinS*but not inT, andjinTbut not inS*, withgain[j] >= gain[i]. Swappingiforjkeeps the size atkand does not decrease the sum. Repeating this turnsS*intoTwithout losing value, soTis optimal. - Negative gains. Alice must take exactly
kchores, so whenkis large some chosen gains may be negative. We take the least negative ones and never filter them out. - Ties. Equal gains are interchangeable and give the same sum.
Edge cases follow naturally: k = 0 gives sum(bob_pay), k = n gives sum(alice_pay), and n = 0 gives 0.
Complexity
- Time:
O(n log n)for the sort (building the gains isO(n)). Usingheapq.nlargestgivesO(n log k), and quickselect gives expectedO(n). - Space:
O(n)for the list of gains.
Reference solution
from typing import List
def max_split_payoff(alice_pay: List[int], bob_pay: List[int], k: int) -> int:
# Baseline: Bob does every chore.
base = sum(bob_pay)
# Change in total when a chore moves from Bob to Alice.
gains = sorted((a - b for a, b in zip(alice_pay, bob_pay)), reverse=True)
# Alice takes exactly k chores: pick the k largest changes,
# including negative ones if necessary.
return base + sum(gains[:k])