We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Minimum Hours of Training to Win a Competition

Number: 2459

Difficulty: Easy

Paid? No

Companies: N/A


Problem Description

You are given initial energy and initial experience along with lists representing opponents' energy and experience. To defeat an opponent, you must have strictly greater energy and experience than the opponent. While winning increases your experience (by the opponent's experience value) and decreases your energy (by the opponent's energy value), you can train before the competition to increase either energy or experience by 1 per hour. The goal is to find the minimum number of training hours required to defeat all opponents in order.


Key Insights

  • To beat all opponents, ensure initial energy is greater than the total energy requirement. Specifically, you need at least (sum(energy) + 1) energy.
  • For experience, since experience increases after each win, simulate the process opponent by opponent.
  • If current experience is not strictly greater than the opponent’s experience, compute the training hours needed, update the current experience, and then continue to the next opponent.
  • Use a greedy approach by simply addressing each deficit as it appears.

Space and Time Complexity

Time Complexity: O(n), where n is the number of opponents, as we iterate through the opponents once. Space Complexity: O(1), since we use only a constant amount of extra space.


Solution

We first calculate the additional hours required for energy by ensuring that our energy exceeds the sum of all opponents’ energy, plus one (since we require strictly greater energy). For experience, we simulate facing each opponent sequentially. At each step, if our current experience is less than or equal to the opponent's experience, we calculate the deficit (opponent’s experience - current experience + 1), add that to our training hours, and update our current experience as if we had trained. Then, after defeating the opponent, we add their experience to our current experience. This greedy simulation approach guarantees that we only train as much as needed.


Code Solutions

def min_training_hours(initialEnergy, initialExperience, energy, experience):
    training_hours = 0

    # Calculate training hours needed for energy.
    total_energy_needed = sum(energy) + 1  # we need strictly greater energy
    if initialEnergy < total_energy_needed:
        training_hours += total_energy_needed - initialEnergy

    # Process each opponent for experience.
    curr_experience = initialExperience
    for i in range(len(experience)):
        # If current experience is not strictly greater than opponent's, train.
        if curr_experience <= experience[i]:
            required = experience[i] - curr_experience + 1
            training_hours += required
            curr_experience += required
        # After winning against opponent, increase current experience.
        curr_experience += experience[i]

    return training_hours

# Example usage:
print(min_training_hours(5, 3, [1,4,3,2], [2,6,3,1]))  # Expected output: 8
← Back to All Questions