Ctrl K

No Back-to-Back Shifts

Medium
A courier is offered side work for the coming days: pay[i] is what they earn by taking the shift on day i. Working two days in a row is not allowed: every shift has to be followed by at least one day off. The courier picks which days to work (any number of days, possibly none). Return the largest total they can earn.
Example 1
pay=[5, 1, 1, 5] → 10

The best choice is the first and the last shift: 5 + 5 = 10, and they are not back to back.

Example 2
pay=[3, 10, 4, 2, 8] → 18

Shifts 1 and 4 (0-based): 10 + 8 = 18. Taking 3 + 4 + 8 = 15 is worse.

Example 3
pay=[0, 0, 7] → 7
Constraints
  • 1 ≤ length of pay ≤ 100
  • 0 ≤ pay[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=[5, 1, 1, 5]
Expected
10

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