Minimum Hours of Training to Win a Competition - Problem

You're preparing for an epic tournament where you'll face n opponents in sequence! ๐Ÿ†

Starting with initialEnergy and initialExperience, you need to strategically train before the competition begins. Each opponent i has specific energy[i] and experience[i] values.

Victory Conditions: To defeat an opponent, you must have strictly greater energy AND experience than them.

Battle Mechanics:

  • โœ… Win: Gain experience[i] points, lose energy[i] points
  • โŒ Lose: Game over - you can't proceed to the next opponent

Training Rules: Each training hour lets you increase either your initial energy OR initial experience by 1.

Goal: Find the minimum training hours needed to defeat all opponents consecutively.

Input & Output

example_1.py โ€” Basic Competition
$ Input: initialEnergy = 5, initialExperience = 3, energy = [1,4,3,2], experience = [2,6,3,1]
โ€บ Output: 8
๐Ÿ’ก Note: You need 8 training hours total. Train energy to 11 (6 hours) and experience to 5 (2 hours) to defeat all opponents: Op1(1,2), Op2(4,6), Op3(3,3), Op4(2,1).
example_2.py โ€” Minimal Training
$ Input: initialEnergy = 2, initialExperience = 4, energy = [1], experience = [3]
โ€บ Output: 0
๐Ÿ’ก Note: You already have enough stats (2 > 1 energy and 4 > 3 experience) to defeat the single opponent, so no training is needed.
example_3.py โ€” High Experience Opponent
$ Input: initialEnergy = 10, initialExperience = 2, energy = [5], experience = [10]
โ€บ Output: 9
๐Ÿ’ก Note: You need to train experience from 2 to 11 (9 hours) to have strictly greater experience than the opponent's 10. Your energy of 10 is already sufficient.

Constraints

  • n == energy.length == experience.length
  • 1 โ‰ค n โ‰ค 100
  • 1 โ‰ค initialEnergy, initialExperience โ‰ค 100
  • 1 โ‰ค energy[i], experience[i] โ‰ค 100
  • All values are positive integers

Visualization

Tap to expand
๐ŸŽฎ RPG Training Strategy๐Ÿ‘ค HeroEnergy: 5Experience: 3Training PlanEnergy +6 hoursExperience +2 hoursTotal: 8 hours๐Ÿ‘น Boss 1Energy: 1Experience: 2Reward: +2 XPCost: -1 Energy๐Ÿ‘น Boss 2Energy: 4Experience: 6Reward: +6 XPCost: -4 Energy๐Ÿ‘น Boss 3Energy: 3Experience: 3Reward: +3 XPCost: -3 Energy๐Ÿง  Strategy Breakdown1. Work backwards for energy: Need 12 total โ†’ Train +72. Simulate forward for experience: Need 7 total โ†’ Train +43. But wait! After Boss 1, we get +2 XP, so only train +24. Minimum training: 6 energy + 2 experience = 8 hoursโœ“Victory!All BossesDefeated
Understanding the Visualization
1
Analyze the Quest Chain
Look at all bosses you must defeat in order
2
Plan Energy Training
Work backwards to ensure you have enough stamina for the entire quest
3
Plan Experience Training
Simulate the quest forward, accounting for XP gains after each victory
4
Execute Optimal Training
Train just enough to guarantee victory with minimum time investment
Key Takeaway
๐ŸŽฏ Key Insight: Energy and experience can be optimized separately since energy decreases while experience increases during battles, allowing us to use a greedy approach for both!
Asked in
Google 12 Amazon 18 Meta 8 Microsoft 6
28.5K Views
Medium Frequency
~15 min Avg. Time
890 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