---
# System prepended metadata

title: 2383. Minimum Hours of Training to Win a Competition
tags: [Leetcode, Easy]

---

# 2383. Minimum Hours of Training to Win a Competition
Link: https://leetcode.com/problems/minimum-hours-of-training-to-win-a-competition/
###### tags: `Leetcode` `Easy`
## 思路
假设起始的```energy```和```experience```都只有0
在过程中看需要加多少的```energy```和```experience```才能战胜每一个对手
最后```addEnergy-initialEnergy``` ```addExp-initialExperience```求和就是答案
如果```initial```比```add```的大直接返回0
注意只有```energy```和```experience```都比对方大的时候才能战胜对方 所以判断的时候要写成```if(initial<=e)```
## Code
```java=
class Solution {
    public int minNumberOfHours(int initialEnergy, int initialExperience, int[] energy, int[] experience) {
        int addEnergy = 0;
        int initial = 0;
        for(int e:energy){
            if(initial<=e){
                int change = e-initial+1;
                addEnergy+=change;
                initial+=change;
            }
            initial-=e;
        }
        int addExp = 0;
        initial = 0;
        for(int e:experience){
            if(initial<=e){
                int change = e-initial+1;
                addExp+=change;
                initial+=change;
            }
            initial += e;
        }
        return Math.max(0,addExp-initialExperience)+Math.max(0, addEnergy-initialEnergy);
    }
}
```