[LeetCode] Maximum Energy Boost From Two Drinks

3259. Maximum Energy Boost From Two Drinks

You are given two integer arrays energyDrinkA and energyDrinkB of the same length n by a futuristic sports scientist. These arrays represent the energy boosts per hour provided by two different energy drinks, A and B, respectively.

You want to maximize your total energy boost by drinking one energy drink per hour. However, if you want to switch from consuming one energy drink to the other, you need to wait for one hour to cleanse your system (meaning you won’t get any energy boost in that hour).

Return the maximum total energy boost you can gain in the next n hours.

Note that you can start consuming either of the two energy drinks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
long long maxEnergyBoost(vector<int>& profitsA, vector<int>& profitsB) {
int n = profitsA.size();
deque<long long> dpA(3), dpB(3);
for(int i = 0; i < n; i++) {
dpA[0] += profitsA[i];
dpB[0] += profitsB[i];
if(i + 2 < n) {
dpB[2] = dpA[0];
dpA[2] = dpB[0];
}
if(i + 1 < n) {
dpA[1] = max(dpA[1], dpA[0]);
dpB[1] = max(dpB[1], dpB[0]);
dpA.push_back(0);
dpB.push_back(0);
dpA.pop_front();
dpB.pop_front();
}
}
return max(dpA[0],dpB[0]);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/08/18/PS/LeetCode/maximum-energy-boost-from-two-drinks/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.