Optimize Work Schedule with Limited Day Swaps
Dynamic ProgrammingAuto-graded · 21 test casesLast updated
Tests dynamic programming with state tracking to maximize earnings from daily pay and streak bonuses while converting limited off-days to work-days.
Problem Statement
You're optimizing a work schedule spanning n days. The schedule is a string where 'W' denotes a scheduled work-day and 'O' denotes an off-day.
Each work-day i yields earnings of pay[i] dollars. Off-days yield nothing. Furthermore, working consecutive days earns a consistency bonus: each pair of adjacent work-days (i, i+1) adds bonus dollars to your total.
You can flip up to k off-days into work-days. Already-scheduled work-days cannot be changed to off-days.
Return the maximum total earnings you can achieve.
Input Format
schedule: string of lengthncontaining only'W'and'O'pay: list ofnnon-negative integers (daily earnings)bonus: non-negative integer (bonus per consecutive work-day pair)k: non-negative integer (maximum flips allowed)
Output Format
Return a single integer: the maximum achievable earnings.
Constraints
1 <= n <= 10000 <= k <= nlen(schedule) == nschedulecontains only'W'and'O'len(pay) == n0 <= pay[i] <= 10^50 <= bonus <= 10^5
Examples
Example 1
Input: optimize_schedule('WOOW', [10, 5, 5, 10], 3, 1)
Output: 28
Example 2
Input: optimize_schedule('OOO', [20, 10, 20], 5, 2)
Output: 40
Hints
Hint 1
Track two states per day: whether the previous day was worked or not, along with how many flips you've used so far.
Hint 2
A scheduled work-day must remain worked and costs zero flips. An off-day can either stay off (no flip) or become worked (uses one flip).
Hint 3
The bonus is only earned when both day i and day i+1 are worked, so transitioning from work-to-work adds the bonus while off-to-work does not.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem requires tracking which days are worked while respecting the flip budget. The key insight is that earnings on day i depend only on:
- Whether day
iis worked (earnspay[i]) - Whether day
i-1was also worked (addsbonusif yes)
This enables a dynamic programming solution with states tracking:
- How many flips have been used (
0tok) - Whether the most recent processed day was worked or off
We maintain two arrays indexed by flips used:
prev_off[flips]: best earnings when the previous day was offprev_work[flips]: best earnings when the previous day was worked
For each day, we compute new states:
If the day is 'O' (off-day):
- Keep it off: transition to
curr_offwith no earnings change - Flip it to work: transition to
curr_work, incrementing flip count, earningpay[i]plusbonusif coming from a work-day
If the day is 'W' (scheduled work-day):
- Must remain worked, no flip cost, earn
pay[i]plusbonusif coming from a work-day
Initialize with prev_off[0] = 0 (before any days, zero flips used, previous state is "off"). All other states start unreachable (large negative sentinel).
The answer is the maximum across all final states.
Correctness
The DP satisfies optimal substructure: given the flip budget remaining and whether the previous day was worked, the best choice for future days is independent of how we reached this state. Scheduled work-days cannot be removed (enforced by having no off-transition for 'W'). Flip constraints are enforced by only incrementing the flip counter on 'O'-to-work transitions and never exceeding k.
Complexity
Time: O(n·k) — We process n days, each with k+1 possible flip counts, doing constant work per state.
Space: O(k) — We only store the previous day's states, rolling them forward each iteration.
Solution Code
def optimize_schedule(schedule: str, pay: list[int], bonus: int, k: int) -> int:
sentinel = -10**30
prev_off = [sentinel] * (k + 1)
prev_work = [sentinel] * (k + 1)
prev_off[0] = 0
for i, day in enumerate(schedule):
curr_off = [sentinel] * (k + 1)
curr_work = [sentinel] * (k + 1)
for flips_used in range(k + 1):
if day == 'O':
# Option 1: Keep this off-day as off
curr_off[flips_used] = max(prev_off[flips_used], prev_work[flips_used])
# Option 2: Flip this off-day to work-day
if flips_used < k:
if prev_off[flips_used] != sentinel:
curr_work[flips_used + 1] = max(
curr_work[flips_used + 1],
prev_off[flips_used] + pay[i]
)
if prev_work[flips_used] != sentinel:
curr_work[flips_used + 1] = max(
curr_work[flips_used + 1],
prev_work[flips_used] + pay[i] + bonus
)
else:
# Scheduled work-day must remain worked
if prev_off[flips_used] != sentinel:
curr_work[flips_used] = max(
curr_work[flips_used],
prev_off[flips_used] + pay[i]
)
if prev_work[flips_used] != sentinel:
curr_work[flips_used] = max(
curr_work[flips_used],
prev_work[flips_used] + pay[i] + bonus
)
prev_off, prev_work = curr_off, curr_work
return max(max(prev_off), max(prev_work))