Ctrl K

Two Projects and the Cost of Switching

Medium
A freelancer is planning the next n hours. There are two projects, and their hourly rates are given by arrays pay_a and pay_b of the same length: working on project A in hour i earns pay_a[i], working on project B earns pay_b[i]. Each hour goes to one project. Either can come first, but switching to the other project costs one hour of catching up, and that hour earns nothing. Return the most money that can be earned over all n hours.
Example 1
pay_a=[6, 2, 1, 1], pay_b=[1, 1, 5, 5] → 16

Work on A in the first hour (6), spend the second hour switching, then do two hours on B (5 + 5), for 16 in total.

Example 2
pay_a=[3, 3, 3], pay_b=[4, 1, 4] → 9

Switching does not pay: A alone gives 9, B alone gives 9, and any switch gives at most 7. The answer is 9.

Constraints
  • n = pay_a.length = pay_b.length
  • 3 ≤ n ≤ 10⁵
  • 1 ≤ pay_a[i], pay_b[i] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFewest Jumps to the End MediumMost Profitable Streak MediumReach the Last Cell MediumWarehouse Cart Routes MediumCheapest Corner-to-Corner Route Medium
loading Python…
Input
pay_a=[6, 2, 1, 1], pay_b=[1, 1, 5, 5]
Expected
16

Run checks the first 3 tests in your browser. Submit checks all 98 tests on the server, hidden ones included.