Advertisement
adityasuman100

getMaxEarnings

Mar 29th, 2025
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. long getMaxEarnings(string schedule, int k, int fixedPay, int bonus) {
  8.     int n = schedule.size();
  9.     vector<int> zeroIndices;
  10.    
  11.     // Store the indices of '0's
  12.     for (int i = 0; i < n; i++) {
  13.         if (schedule[i] == '0') {
  14.             zeroIndices.push_back(i);
  15.         }
  16.     }
  17.    
  18.     // If k is greater than or equal to the number of '0's, make all '0's '1'
  19.     int zeroCount = zeroIndices.size();
  20.     if (k >= zeroCount) {
  21.         fill(schedule.begin(), schedule.end(), '1');
  22.     } else {
  23.         for (int i = 0; i < k; i++) {
  24.             schedule[zeroIndices[i]] = '1';
  25.         }
  26.     }
  27.    
  28.     // Calculate earnings
  29.     long earnings = 0;
  30.     if (schedule[0] == '1') {
  31.         earnings += fixedPay;
  32.     }
  33.    
  34.     for (int i = 1; i < n; i++) {
  35.         if (schedule[i] == '1') {
  36.             earnings += fixedPay;
  37.             if (schedule[i - 1] == '1') {
  38.                 earnings += bonus;
  39.             }
  40.         }
  41.     }
  42.    
  43.     return earnings;
  44. }
  45.  
  46. int main() {
  47.     cout << getMaxEarnings("10100", 2, 1, 2) << endl; // Output: 10
  48.     cout << getMaxEarnings("100101", 2, 4, 3) << endl; // Output: 29
  49.     cout << getMaxEarnings("1111001", 1, 3, 3) << endl; // Output: 30
  50.     return 0;
  51. }
  52.  
  53.  
  54.  
  55.  
  56.  
  57. 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.
  58.  
  59. 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").
  60.  
  61. Find the maximum earnings the employee can get after changing at most k days from 'O' to '1'.
  62.  
  63. Example n =5
  64.  
  65. k= 2
  66.  
  67. fixedPay = 1
  68.  
  69. bonus = 2
  70.  
  71. schedule ="10100"
  72.  
  73. An optimal way to change the schedule is to work on the second and fourth days. In this case, the schedule becomes "11110".
  74.  
  75. On the first day, earnings - fixedPay = 1.
  76.  
  77. On days 2, 3, and 4, earnings - fixedPay + bonus = 3.
  78.  
  79. Total earnings = 1 + 3 + 3 + 3 + 0 = 10. Return 10.
  80.  
  81. Function Description
  82.  
  83. Complete the function getMaxEarnings in the editor with the following parameters:
  84.  
  85. string schedule: initial work schedule
  86.  
  87. int k: the maximum number of extra days the employee can work int fixedPay: the fixed pay for each workday
  88.  
  89. int bonus: the bonus if they also worked the previous day
  90.  
  91. Returns
  92.  
  93. long: the maximum earnings possible
  94.  
  95. sample 0
  96.  
  97. schedule = "100101"
  98.  
  99. k = 2
  100.  
  101. fixedPay = 4
  102.  
  103. bonus = 3
  104.  
  105. output
  106.  
  107. 29
  108.  
  109. An optimal schedule is to work on the second and third days too, so the schedule is "111101".
  110.  
  111. On the first day, earnings = fixedPay = 4.
  112.  
  113. On days 2, 3, and 4, earnings = fixedPay + bonus = 7.
  114.  
  115. On day 5, earnings - fixedPay = 4.
  116.  
  117. Total earnings = 4 + 7 + 7 + 7 + 0 + 4 = 29.
  118.  
  119. sample 1
  120.  
  121. schedule = "1111001"
  122.  
  123. k = 1
  124.  
  125. fixedPay = 3
  126.  
  127. bonus = 3
  128.  
  129. output
  130.  
  131. 30
  132.  
  133. Explanation
  134.  
  135. An optimal schedule is to also work on the fifth day, so the schedule is *1111101".
  136.  
  137. On the first day, earnings = fixedPay = 3.
  138.  
  139. On days 2, 3, 4, and 5 earnings - fixedPay + bonus - 6.
  140.  
  141. On day 6, earnings = fixedPay = 3.
  142.  
  143. Total earnings = 3 + 6 + 6 + 6 + 6 + 0 + 3 = 30.
  144.  
  145. solve in c++
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement