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
๐Ÿƒโ€โ™‚๏ธ Athlete's Energy OptimizationHour 0: StartA: 4 โšก | B: 1 โšกHour 1: DecisionA: 1 โšก | B: 3 โšกHour 2: FinalA: 1 โšก | B: 1 โšก4Choose A5Continue A6Continue A4Switch penalty๐ŸŽฏ Maximum Energy: 6 unitsStrategy: Stay with drink A throughout
Understanding the Visualization
1
Initial Choice
Hour 0: Choose between drink A (4 energy) or B (1 energy) - A is better
2
Continue vs Switch
Hour 1: Continue A (4+1=5) or switch to B (penalty, so just 1) - Continue A
3
Optimal Decision
Hour 2: Continue A (5+1=6) or switch to B (1+1=2) - Continue A wins
4
Final Result
Maximum energy achieved: 6 units by staying with drink A throughout
Key Takeaway
๐ŸŽฏ Key Insight: Dynamic programming tracks the optimal energy for each drink choice, efficiently handling the switching penalty by considering both continuation and switching options at each step.
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