KatInterview

Split Chores Between Two Workers With a Fixed Quota

Greedy & IntervalsAuto-graded · 21 test cases
EasyAuto-gradedGreedy & Intervalsgreedysortingarrays

Last 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 chore i.
  • bob_pay[i] is the payoff if Bob does chore i.

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^5
  • alice_pay and bob_pay both have length n
  • 0 <= 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 n up to 10^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.