2383. 赢得比赛需要的最少训练时长

leetcode-2383. 赢得比赛需要的最少训练时长

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public:
int minNumberOfHours(int initialEnergy, int initialExperience, vector<int>& energy, vector<int>& experience) {
int sumExperience = initialExperience;
int sumEnergy = 0;
int result = 0;
for(int num : experience)
{
if(sumExperience <= num)
{
result += (num + 1 -sumExperience);
sumExperience = num + 1;
}
else
sumExperience += num;
}
for(int num : energy)
{
sumEnergy += num;
}
if(sumEnergy >= initialEnergy)
result += (sumEnergy + 1 - initialEnergy);
return result;
}
};