Problem · Array

Maximum Payout with Context Switches

Learn this problem
HardPhonePe logoPhonePeFULLTIMEOA

Problem statement

You are given two arrays, payoutA and payoutB, of equal length. Index i represents one task slot in a fixed sequence. In that slot, you may skip the slot, complete Startup A's task for payoutA[i], or complete Startup B's task for payoutB[i].

You may complete at most maxTasks tasks. Every completed task costs one day. If a completed task belongs to a different startup than the previous completed task, it also costs one idle context-switch day. Skipped slots consume no days and do not change the startup of the previous completed task.

The total number of task days plus context-switch days must not exceed days. Return the maximum total payout. Choosing no tasks is allowed and yields 0.

Function

maximumPayout(payoutA: int[], payoutB: int[], maxTasks: int, days: int) → long

Examples

Example 1

payoutA = [5,1,10]payoutB = [1,8,2]maxTasks = 2days = 2return = 15

Complete Startup A's tasks in slots 0 and 2. They cost two task days, require no context switch, and pay 5 + 10 = 15.

Example 2

payoutA = [5,1,10]payoutB = [1,8,2]maxTasks = 2days = 3return = 18

Complete Startup B's task in slot 1 and Startup A's task in slot 2. The two tasks and one switch consume three days, and the payout is 8 + 10 = 18.

Example 3

payoutA = [7,4]payoutB = [6,9]maxTasks = 0days = 4return = 0

No task may be completed when maxTasks is 0, so the maximum payout is 0.

Constraints

  • 1 <= payoutA.length = payoutB.length <= 100
  • 0 <= payoutA[i], payoutB[i] <= 10^9
  • 0 <= maxTasks <= payoutA.length
  • 0 <= days <= 2 * payoutA.length

More PhonePe problems

drafts saved locally
public long maximumPayout(int[] payoutA, int[] payoutB, int maxTasks, int days) {
  // write your code here
}
payoutA[5,1,10]
payoutB[1,8,2]
maxTasks2
days2
expected15
checking account