Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- long getMaxEarnings(string schedule, int k, int fixedPay, int bonus) {
- int n = schedule.size();
- vector<int> zeroIndices;
- // Store the indices of '0's
- for (int i = 0; i < n; i++) {
- if (schedule[i] == '0') {
- zeroIndices.push_back(i);
- }
- }
- // If k is greater than or equal to the number of '0's, make all '0's '1'
- int zeroCount = zeroIndices.size();
- if (k >= zeroCount) {
- fill(schedule.begin(), schedule.end(), '1');
- } else {
- for (int i = 0; i < k; i++) {
- schedule[zeroIndices[i]] = '1';
- }
- }
- // Calculate earnings
- long earnings = 0;
- if (schedule[0] == '1') {
- earnings += fixedPay;
- }
- for (int i = 1; i < n; i++) {
- if (schedule[i] == '1') {
- earnings += fixedPay;
- if (schedule[i - 1] == '1') {
- earnings += bonus;
- }
- }
- }
- return earnings;
- }
- int main() {
- cout << getMaxEarnings("10100", 2, 1, 2) << endl; // Output: 10
- cout << getMaxEarnings("100101", 2, 4, 3) << endl; // Output: 29
- cout << getMaxEarnings("1111001", 1, 3, 3) << endl; // Output: 30
- return 0;
- }
- In a company, an employee receives a fixed amount, fixedPay dollars, each day they work. They also receive a bonus, bonus dollars, on a workday if they worked the day before. The employee is planning their work schedule for n days.
- Their schedule is given as a binary string schedule where '1" is a workday, and 'O" is not. The employee can change up to k days off ('0) to workdays (*1").
- Find the maximum earnings the employee can get after changing at most k days from 'O' to '1'.
- Example n =5
- k= 2
- fixedPay = 1
- bonus = 2
- schedule ="10100"
- An optimal way to change the schedule is to work on the second and fourth days. In this case, the schedule becomes "11110".
- On the first day, earnings - fixedPay = 1.
- On days 2, 3, and 4, earnings - fixedPay + bonus = 3.
- Total earnings = 1 + 3 + 3 + 3 + 0 = 10. Return 10.
- Function Description
- Complete the function getMaxEarnings in the editor with the following parameters:
- string schedule: initial work schedule
- int k: the maximum number of extra days the employee can work int fixedPay: the fixed pay for each workday
- int bonus: the bonus if they also worked the previous day
- Returns
- long: the maximum earnings possible
- sample 0
- schedule = "100101"
- k = 2
- fixedPay = 4
- bonus = 3
- output
- 29
- An optimal schedule is to work on the second and third days too, so the schedule is "111101".
- On the first day, earnings = fixedPay = 4.
- On days 2, 3, and 4, earnings = fixedPay + bonus = 7.
- On day 5, earnings - fixedPay = 4.
- Total earnings = 4 + 7 + 7 + 7 + 0 + 4 = 29.
- sample 1
- schedule = "1111001"
- k = 1
- fixedPay = 3
- bonus = 3
- output
- 30
- Explanation
- An optimal schedule is to also work on the fifth day, so the schedule is *1111101".
- On the first day, earnings = fixedPay = 3.
- On days 2, 3, 4, and 5 earnings - fixedPay + bonus - 6.
- On day 6, earnings = fixedPay = 3.
- Total earnings = 3 + 6 + 6 + 6 + 6 + 0 + 3 = 30.
- solve in c++
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement