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, loseenergy[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
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!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code