leetcodedaybyday - Beats 100% with C++ and Beats 100% with Python3
IntuitionThe problem involves calculating the minimum number of training hours required to gain enough energy and experience to defeat all opponents in sequence
# Intuition
The problem involves calculating the minimum number of training hours required to gain enough energy and experience to defeat all opponents in sequence. The two key components are ensuring sufficient energy for all battles combined and gaining enough experience for each battle individually.

# Approach
1. **Energy Training**:
   - Calculate the total energy required to fight all opponents (`energyLose`).
   - Determine the additional energy needed by subtracting `initialEnergy` from `energyLose` and adding 1 (to exceed the required energy). If no extra energy is required, set it to 0.

2. **Experience Training**:
   - Traverse the `experience` array. For each opponent:
     - Check if the current experience is insufficient to win. If so, calculate and accumulate the training hours required to exceed the opponent's experience.
     - Add the opponent's experience to the current experience after training (if needed).

3. **Sum Results**:
   - Add the training hours for energy and experience to obtain the final result.

# Complexity
- **Time Complexity**:  
  \(O(n)\), where \(n\) is the length of the `energy` or `experience` array. The calculation involves a single pass through each array.

- **Space Complexity**:  
  \(O(1)\), as the solution uses a constant amount of extra space.
---
# Code
```cpp []
class Solution {
public:
    int minNumberOfHours(int initialEnergy, int initialExperience, vector<int>& energy, vector<int>& experience) {
        int energyLose = 0;
        for (int e : energy){
            energyLose += e;
        }
        int availableEnergy = max(0, energyLose - initialEnergy + 1);
        int experienceEarn = 0;
        for (int exp : experience){
            if(initialExperience <= exp){
                experienceEarn += (exp - initialExperience + 1);
                initialExperience += (exp - initialExperience + 1);
            }
            initialExperience += exp;
        }
        return availableEnergy + experienceEarn;
    }
};


```
```python3 []
class Solution:
    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:
        energyLose = sum(energy)

        availableEnergy = max(0, energyLose - initialEnergy + 1)
        
        experienceEarn = 0
        
        for exp in experience:
            if initialExperience <= exp:
                experienceEarn += (exp - initialExperience +1)
                initialExperience += (exp - initialExperience +1)
            initialExperience += exp
        
        return availableEnergy + experienceEarn



```


['Array', 'Greedy', 'C++', 'Python3']