Minimum Hours of Training to Win a Competition - Problem

You are entering a competition, and are given two positive integers initialEnergy and initialExperience denoting your initial energy and initial experience respectively.

You are also given two 0-indexed integer arrays energy and experience, both of length n.

You will face n opponents in order. The energy and experience of the i-th opponent is denoted by energy[i] and experience[i] respectively. When you face an opponent, you need to have both strictly greater experience and energy to defeat them and move to the next opponent if available.

Defeating the i-th opponent increases your experience by experience[i], but decreases your energy by energy[i].

Before starting the competition, you can train for some number of hours. After each hour of training, you can either choose to increase your initial experience by one, or increase your initial energy by one.

Return the minimum number of training hours required to defeat all n opponents.

Input & Output

Example 1 — Basic Training Need
$ Input: initialEnergy = 5, initialExperience = 3, energy = [1,4,3,2], experience = [2,6,3,1]
Output: 8
💡 Note: Need experience > 2 for first opponent (have 3, OK). Need energy > 4 for second opponent after losing 1 energy, so need 1 more. Need experience > 6 for second opponent after gaining 2 experience (have 5), so need 2 more. Continue simulation to get total 8 training hours.
Example 2 — No Training Needed
$ Input: initialEnergy = 20, initialExperience = 15, energy = [1,2,3], experience = [1,2,3]
Output: 0
💡 Note: Already have enough energy and experience to defeat all opponents without any training.
Example 3 — Single Opponent
$ Input: initialEnergy = 1, initialExperience = 1, energy = [1], experience = [1]
Output: 2
💡 Note: Need energy > 1 and experience > 1. Currently have energy=1, experience=1, so need 1 hour each for energy and experience training.

Constraints

  • n == energy.length == experience.length
  • 1 ≤ n ≤ 100
  • 1 ≤ initialEnergy, initialExperience ≤ 100
  • 1 ≤ energy[i], experience[i] ≤ 100

Visualization

Tap to expand
Minimum Hours of Training to Win Competition INPUT Initial Values: Energy = 5 Experience = 3 energy[] array: 1 4 3 2 [0] [1] [2] [3] experience[] array: 2 6 3 1 [0] [1] [2] [3] 4 Opponents to defeat: O1 O2 O3 O4 ALGORITHM STEPS 1 Calculate Energy Needed Sum all energy: 1+4+3+2=10 Need: 10+1=11 (strictly >) Training: 11-5 = 6 hours 2 Simulate Experience Track exp through battles Must be strictly greater 3 Experience Simulation Opp Need Have Train After O1 >2 3 0 3+2=5 O2 >6 5 +2 7+6=13 O3 >3 13 0 13+3=16 O4 >1 16 0 16+1=17 Exp training needed: 2 hours 4 Sum Training Hours Energy: 6 + Experience: 2 Total = 8 FINAL RESULT Training Distribution: Energy: 6 hours 2h Experience OUTPUT 8 Verification: After Training: Energy: 5+6 = 11 Experience: 3+2 = 5 O1: 11>1 OK, 5>2 OK O2: 10>4 OK, 7>6 OK O3: 6>3 OK, 13>3 OK O4: 3>2 OK, 16>1 OK Key Insight: 1. Energy is simple: need strictly greater than sum of all opponents' energy (total+1). 2. Experience requires greedy simulation: before each battle, train if current exp <= opponent's exp. 3. After defeating opponent, experience accumulates, reducing future training needs. TutorialsPoint - Minimum Hours of Training to Win a Competition | Greedy Simulation Approach
Asked in
Amazon 15 Microsoft 8
12.0K Views
Medium Frequency
~15 min Avg. Time
450 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