KatInterview

Optimize Work Schedule with Limited Day Swaps

Dynamic ProgrammingAuto-graded · 21 test cases
MediumAuto-gradedDynamic Programmingdynamic-programmingstate-machineoptimization

Last 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 length n containing only 'W' and 'O'
  • pay: list of n non-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 <= 1000
  • 0 <= k <= n
  • len(schedule) == n
  • schedule contains only 'W' and 'O'
  • len(pay) == n
  • 0 <= pay[i] <= 10^5
  • 0 <= 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.