Maximum Energy Boost From Two Drinks - Problem

Imagine you're a professional athlete preparing for the most important competition of your career! 🏃‍♂️💪

A futuristic sports scientist has provided you with two different energy drinks A and B, each offering varying energy boosts throughout the day. You have n hours to prepare, and you must drink exactly one energy drink per hour to maximize your total energy boost.

Here's the catch: switching between drinks isn't instant! If you want to switch from one energy drink to another, your body needs one full hour to cleanse your system - meaning you get zero energy boost during that transition hour.

Your Goal: Determine the maximum total energy boost you can achieve over n hours. You can start with either drink A or drink B.

Input: Two integer arrays energyDrinkA and energyDrinkB of length n, representing energy boosts per hour.

Output: The maximum total energy boost possible.

Input & Output

example_1.py — Basic case
$ Input: energyDrinkA = [1, 3, 1], energyDrinkB = [3, 1, 1]
Output: 5
💡 Note: Start with B (3), switch to A in hour 2 but get 0 due to switching penalty, then get A (1) in hour 3. Total: 3 + 0 + 1 = 4. Or start with B (3), continue with B (1), continue with B (1). Total: 3 + 1 + 1 = 5. The maximum is 5.
example_2.py — No switching optimal
$ Input: energyDrinkA = [4, 1, 1], energyDrinkB = [1, 1, 3]
Output: 7
💡 Note: Start with A (4), switch to B (penalty = 0), then B (3). Total: 4 + 0 + 3 = 7. This beats staying with one drink throughout.
example_3.py — Single hour
$ Input: energyDrinkA = [5], energyDrinkB = [3]
Output: 5
💡 Note: With only one hour, choose the higher energy drink A with value 5.

Constraints

  • n == energyDrinkA.length == energyDrinkB.length
  • 1 ≤ n ≤ 105
  • 1 ≤ energyDrinkA[i], energyDrinkB[i] ≤ 105

Visualization

Tap to expand
Maximum Energy Boost From Two Drinks INPUT energyDrinkA: 1 3 1 energyDrinkB: 3 1 1 Hour 0 Hour 1 Hour 2 n = 3 hours Rules: - Drink 1 per hour - Switch = 1 hour cooldown - Start with A or B ALGORITHM STEPS 1 Initialize DP dpA[0]=A[0], dpB[0]=B[0] 2 State Transition Stay or switch (skip hour) 3 Recurrence dpA[i]=max(dpA[i-1],dpB[i-2])+A[i] 4 Final Answer max(dpA[n-1], dpB[n-1]) DP Table: i 0 1 2 dpA 1 4 5 dpB 3 4 5 max(5, 5) = 5 FINAL RESULT Optimal Path: B[0] +3 B[1] +1 B[2] +1 Alternative: A[0] --> A[1] --> A[2] = 1 + 3 + 1 = 5 Calculation: 3 + 1 + 1 = 5 (No switches needed) Output: 5 [OK] Maximum Energy Boost Key Insight: Use Dynamic Programming with two states: dpA[i] = max energy ending at hour i with drink A, dpB[i] = max energy ending at hour i with drink B. Switching costs 1 hour (use i-2 for switches). TutorialsPoint - Maximum Energy Boost From Two Drinks | Optimal Solution
Asked in
Google 42 Amazon 38 Meta 29 Microsoft 31
23.5K Views
High Frequency
~18 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen