Maximum Energy Boost From Two Drinks is a Dynamic Programming problem where you have two energy drinks, A and B. For each hour, you get a certain boost from drinking A or B. However, if you want to switch from drink A to B (or vice versa), you must skip one hour to make the transition. The goal is to maximize the total energy boost over a fixed number of hours.
Microsoft uses this problem to test a candidate's ability to handle state transitions in Dynamic Programming. It's a classic 'decision-making over time' problem. It tests whether you can correctly define the DP state to include not just the current hour, but also which drink you were consuming in the previous hour.
This problem follows the Dynamic Programming interview pattern. You maintain two DP arrays (or variables): dpA[i] and dpB[i].
dpA[i] = EnergyBoostA[i] + max(dpA[i-1], dpB[i-2])dpB[i] = EnergyBoostB[i] + max(dpB[i-1], dpA[i-2])
This reflects that to have drink A at hour i, you either had it at i-1 or you switched from B, which requires you to have had B at i-2 (skipping i-1).Drink A boosts: [4, 1, 1] Drink B boosts: [1, 1, 5]
A frequent error is forgetting the 'skip an hour' rule, leading to a simple greedy choice at each hour. Another is not properly initializing the DP values for the first two hours, which can lead to index-out-of-bounds errors or incorrect base cases. Some might also try to use a 3D DP which is unnecessarily complex.
When a problem involves 'switching costs' or 'wait times' between states, think about how the previous state i-k affects the current state i. Drawing a state transition diagram can help clarify which previous values are accessible.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Last Stone Weight II | Medium | Solve | |
| Uncrossed Lines | Medium | Solve | |
| Find the Maximum Length of Valid Subsequence I | Medium | Solve | |
| Maximum Absolute Sum of Any Subarray | Medium | Solve | |
| Maximum Subarray Sum with One Deletion | Medium | Solve |