2383. Minimum Hours of Training to Win a Competition LeetCode Solution

In this guide, you will get 2383. Minimum Hours of Training to Win a Competition LeetCode Solution with the best time and space complexity. The solution to Minimum Hours of Training to Win a Competition problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Hours of Training to Win a Competition solution in C++
  4. Minimum Hours of Training to Win a Competition solution in Java
  5. Minimum Hours of Training to Win a Competition solution in Python
  6. Additional Resources
2383. Minimum Hours of Training to Win a Competition LeetCode Solution image

Problem Statement of Minimum Hours of Training to Win a Competition

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 ith 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 ith 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.

Example 1:

Input: initialEnergy = 5, initialExperience = 3, energy = [1,4,3,2], experience = [2,6,3,1]
Output: 8
Explanation: You can increase your energy to 11 after 6 hours of training, and your experience to 5 after 2 hours of training.
You face the opponents in the following order:
– You have more energy and experience than the 0th opponent so you win.
Your energy becomes 11 – 1 = 10, and your experience becomes 5 + 2 = 7.
– You have more energy and experience than the 1st opponent so you win.
Your energy becomes 10 – 4 = 6, and your experience becomes 7 + 6 = 13.
– You have more energy and experience than the 2nd opponent so you win.
Your energy becomes 6 – 3 = 3, and your experience becomes 13 + 3 = 16.
– You have more energy and experience than the 3rd opponent so you win.
Your energy becomes 3 – 2 = 1, and your experience becomes 16 + 1 = 17.
You did a total of 6 + 2 = 8 hours of training before the competition, so we return 8.
It can be proven that no smaller answer exists.

Example 2:

Input: initialEnergy = 2, initialExperience = 4, energy = [1], experience = [3]
Output: 0
Explanation: You do not need any additional energy or experience to win the competition, so we return 0.

Constraints:

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2383. Minimum Hours of Training to Win a Competition LeetCode Solution in C++

class Solution {
 public:
  int minNumberOfHours(int initialEnergy, int initialExperience,
                       vector<int>& energy, vector<int>& experience) {
    return getRequiredEnergy(initialEnergy, energy) +
           getRequiredExperience(initialExperience, experience);
  }

 private:
  int getRequiredEnergy(int initialEnergy, const vector<int>& energy) {
    return max(0,
               accumulate(energy.begin(), energy.end(), 0) + 1 - initialEnergy);
  }

  int getRequiredExperience(int currentExperience,
                            const vector<int>& experience) {
    int requiredExperience = 0;
    for (const int e : experience) {
      if (e >= currentExperience) {
        requiredExperience += e + 1 - currentExperience;
        currentExperience += e + 1 - currentExperience;
      }
      currentExperience += e;
    }
    return requiredExperience;
  }
};
/* code provided by PROGIEZ */

2383. Minimum Hours of Training to Win a Competition LeetCode Solution in Java

class Solution {
  public int minNumberOfHours(int initialEnergy, int initialExperience, int[] energy,
                              int[] experience) {
    return getRequiredEnergy(initialEnergy, energy) +
        getRequiredExperience(initialExperience, experience);
  }

  private int getRequiredEnergy(int initialEnergy, int[] energy) {
    return Math.max(0, Arrays.stream(energy).sum() + 1 - initialEnergy);
  }

  private int getRequiredExperience(int currentExperience, int[] experience) {
    int requiredExperience = 0;
    for (final int e : experience) {
      if (e >= currentExperience) {
        requiredExperience += e + 1 - currentExperience;
        currentExperience += e + 1 - currentExperience;
      }
      currentExperience += e;
    }
    return requiredExperience;
  }
}
// code provided by PROGIEZ

2383. Minimum Hours of Training to Win a Competition LeetCode Solution in Python

class Solution:
  def minNumberOfHours(
      self,
      initialEnergy: int,
      initialExperience: int,
      energy: list[int],
      experience: list[int],
  ) -> int:
    return (self._getRequiredEnergy(initialEnergy, energy) +
            self._getRequiredExperience(initialExperience, experience))

  def _getRequiredEnergy(self, initialEnergy: int, energy: list[int]) -> int:
    return max(0, sum(energy) + 1 - initialEnergy)

  def _getRequiredExperience(
      self,
      currentExperience: int,
      experience: list[int],
  ) -> int:
    requiredExperience = 0
    for e in experience:
      if e >= currentExperience:
        requiredExperience += e + 1 - currentExperience
        currentExperience += e + 1 - currentExperience
      currentExperience += e
    return requiredExperience
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.