[LeetCode] Minimum Hours of Training to Win a Competition

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
int minNumberOfHours(int er, int ep, vector<int>& A, vector<int>& B) {
int res = max(accumulate(begin(A), end(A), 0) - er + 1, 0), n = A.size();
for(int i = 0; i < n; i++) {
int nep = B[i];
if(ep > nep) {
ep += nep;
} else {
res += nep - ep + 1;
ep = nep * 2 + 1;
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/08/21/PS/LeetCode/minimum-hours-of-training-to-win-a-competition/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.