Problem · Array

Maximize Workday Earnings

Learn this problem
MediumCitadel logoCitadelINTERNOA

Problem statement

You are given a work schedule schedule. Each character is W for a workday or O for a day off.

  • Every workday earns dailyPay.
  • A workday also earns consecutiveBonus when the immediately preceding day is a workday.
  • You may change at most k occurrences of O into W.

Return the maximum total earnings after the changes. Both pay values are nonnegative, so changing an additional available day off can never reduce the result.

Function

maximizeEarnings(schedule: String, k: int, dailyPay: long, consecutiveBonus: long) → long

Examples

Example 1

schedule = "WOWO"k = 1dailyPay = 10consecutiveBonus = 5return = 40

Changing the middle day off produces WWWO. Three workdays earn 30, and the two adjacent workday pairs earn 10 in bonuses.

Example 2

schedule = "OOOO"k = 2dailyPay = 10consecutiveBonus = 5return = 25

Choose two adjacent days. The two workdays earn 20, and their one adjacency earns a bonus of 5.

Example 3

schedule = "WWOOOW"k = 2dailyPay = 10consecutiveBonus = 5return = 65

Use both changes next to the leading run, producing four consecutive workdays and one separated workday. Five workdays earn 50, and three adjacencies earn 15.

Constraints

  • 1 <= schedule.length <= 200000
  • schedule[i] is W or O.
  • 0 <= k <= schedule.length
  • 0 <= dailyPay, consecutiveBonus <= 10^9
  • The answer fits in a signed 64-bit integer.

More Citadel problems

drafts saved locally
public long maximizeEarnings(String schedule, int k, long dailyPay, long consecutiveBonus) {
  // write your code here
}
schedule"WOWO"
k1
dailyPay10
consecutiveBonus5
expected40
checking account