comments | difficulty | edit_url | rating | source | tags | ||
---|---|---|---|---|---|---|---|
true |
简单 |
1413 |
第 307 场周赛 Q1 |
|
你正在参加一场比赛,给你两个 正 整数 initialEnergy
和 initialExperience
分别表示你的初始精力和初始经验。
另给你两个下标从 0 开始的整数数组 energy
和 experience
,长度均为 n
。
你将会 依次 对上 n
个对手。第 i
个对手的精力和经验分别用 energy[i]
和 experience[i]
表示。当你对上对手时,需要在经验和精力上都 严格 超过对手才能击败他们,然后在可能的情况下继续对上下一个对手。
击败第 i
个对手会使你的经验 增加 experience[i]
,但会将你的精力 减少 energy[i]
。
在开始比赛前,你可以训练几个小时。每训练一个小时,你可以选择将增加经验增加 1 或者 将精力增加 1 。
返回击败全部 n
个对手需要训练的 最少 小时数目。
示例 1:
输入:initialEnergy = 5, initialExperience = 3, energy = [1,4,3,2], experience = [2,6,3,1] 输出:8 解释:在 6 小时训练后,你可以将精力提高到 11 ,并且再训练 2 个小时将经验提高到 5 。 按以下顺序与对手比赛: - 你的精力与经验都超过第 0 个对手,所以获胜。 精力变为:11 - 1 = 10 ,经验变为:5 + 2 = 7 。 - 你的精力与经验都超过第 1 个对手,所以获胜。 精力变为:10 - 4 = 6 ,经验变为:7 + 6 = 13 。 - 你的精力与经验都超过第 2 个对手,所以获胜。 精力变为:6 - 3 = 3 ,经验变为:13 + 3 = 16 。 - 你的精力与经验都超过第 3 个对手,所以获胜。 精力变为:3 - 2 = 1 ,经验变为:16 + 1 = 17 。 在比赛前进行了 8 小时训练,所以返回 8 。 可以证明不存在更小的答案。
示例 2:
输入:initialEnergy = 2, initialExperience = 4, energy = [1], experience = [3] 输出:0 解释:你不需要额外的精力和经验就可以赢得比赛,所以返回 0 。
提示:
n == energy.length == experience.length
1 <= n <= 100
1 <= initialEnergy, initialExperience, energy[i], experience[i] <= 100
对上对手时,需要满足经验和精力都严格超过对手,因此,我们遍历
遍历结束,返回训练的小时数。
时间复杂度
class Solution:
def minNumberOfHours(
self,
initialEnergy: int,
initialExperience: int,
energy: List[int],
experience: List[int],
) -> int:
ans = 0
for a, b in zip(energy, experience):
if initialEnergy <= a:
ans += a - initialEnergy + 1
initialEnergy = a + 1
if initialExperience <= b:
ans += b - initialExperience + 1
initialExperience = b + 1
initialEnergy -= a
initialExperience += b
return ans
class Solution {
public int minNumberOfHours(
int initialEnergy, int initialExperience, int[] energy, int[] experience) {
int ans = 0;
for (int i = 0; i < energy.length; ++i) {
int a = energy[i], b = experience[i];
if (initialEnergy <= a) {
ans += a - initialEnergy + 1;
initialEnergy = a + 1;
}
if (initialExperience <= b) {
ans += b - initialExperience + 1;
initialExperience = b + 1;
}
initialEnergy -= a;
initialExperience += b;
}
return ans;
}
}
class Solution {
public:
int minNumberOfHours(int initialEnergy, int initialExperience, vector<int>& energy, vector<int>& experience) {
int ans = 0;
for (int i = 0; i < energy.size(); ++i) {
int a = energy[i], b = experience[i];
if (initialEnergy <= a) {
ans += a - initialEnergy + 1;
initialEnergy = a + 1;
}
if (initialExperience <= b) {
ans += b - initialExperience + 1;
initialExperience = b + 1;
}
initialEnergy -= a;
initialExperience += b;
}
return ans;
}
};
func minNumberOfHours(initialEnergy int, initialExperience int, energy []int, experience []int) int {
ans := 0
for i, a := range energy {
b := experience[i]
if initialEnergy <= a {
ans += a - initialEnergy + 1
initialEnergy = a + 1
}
if initialExperience <= b {
ans += b - initialExperience + 1
initialExperience = b + 1
}
initialEnergy -= a
initialExperience += b
}
return ans
}
function minNumberOfHours(
initialEnergy: number,
initialExperience: number,
energy: number[],
experience: number[],
): number {
const n = energy.length;
let ans = 0;
for (let i = 0; i < n; i++) {
const minEnergy = energy[i];
const minExperience = experience[i];
if (initialEnergy <= minEnergy) {
const need = minEnergy - initialEnergy + 1;
ans += need;
initialEnergy += need;
}
if (initialExperience <= minExperience) {
const need = minExperience - initialExperience + 1;
ans += need;
initialExperience += need;
}
initialEnergy -= minEnergy;
initialExperience += minExperience;
}
return ans;
}
impl Solution {
pub fn min_number_of_hours(
mut initial_energy: i32,
mut initial_experience: i32,
energy: Vec<i32>,
experience: Vec<i32>,
) -> i32 {
let n = energy.len();
let mut res = 0;
for i in 0..n {
if initial_energy <= energy[i] {
res += energy[i] - initial_energy + 1;
initial_energy = energy[i] + 1;
}
if initial_experience <= experience[i] {
res += experience[i] - initial_experience + 1;
initial_experience = experience[i] + 1;
}
initial_energy -= energy[i];
initial_experience += experience[i];
}
res
}
}
int minNumberOfHours(int initialEnergy, int initialExperience, int* energy, int energySize, int* experience, int experienceSize) {
int res = 0;
for (int i = 0; i < energySize; i++) {
if (initialEnergy <= energy[i]) {
res += energy[i] - initialEnergy + 1;
initialEnergy = energy[i] + 1;
}
if (initialExperience <= experience[i]) {
res += experience[i] - initialExperience + 1;
initialExperience = experience[i] + 1;
}
initialEnergy -= energy[i];
initialExperience += experience[i];
}
return res;
}
我们可以先在初始时,把精力直接补充到足够击败这
接下来我们只需考虑经验值的问题。遍历
遍历结束,返回训练的小时数。
时间复杂度
class Solution:
def minNumberOfHours(
self,
initialEnergy: int,
initialExperience: int,
energy: List[int],
experience: List[int],
) -> int:
ans = max(0, sum(energy) - initialEnergy + 1)
for x in experience:
if initialExperience <= x:
ans += x - initialExperience + 1
initialExperience = x + 1
initialExperience += x
return ans
class Solution {
public int minNumberOfHours(
int initialEnergy, int initialExperience, int[] energy, int[] experience) {
int s = 0;
for (int x : energy) {
s += x;
}
int ans = Math.max(0, s - initialEnergy + 1);
for (int x : experience) {
if (initialExperience <= x) {
ans += x - initialExperience + 1;
initialExperience = x + 1;
}
initialExperience += x;
}
return ans;
}
}
class Solution {
public:
int minNumberOfHours(int initialEnergy, int initialExperience, vector<int>& energy, vector<int>& experience) {
int s = accumulate(energy.begin(), energy.end(), 0);
int ans = max(0, s - initialEnergy + 1);
for (int x : experience) {
if (initialExperience <= x) {
ans += x - initialExperience + 1;
initialExperience = x + 1;
}
initialExperience += x;
}
return ans;
}
};
func minNumberOfHours(initialEnergy int, initialExperience int, energy []int, experience []int) (ans int) {
s := 0
for _, x := range energy {
s += x
}
if y := s - initialEnergy + 1; y > 0 {
ans = y
}
for _, x := range experience {
if initialExperience <= x {
ans += x - initialExperience + 1
initialExperience = x + 1
}
initialExperience += x
}
return
}
function minNumberOfHours(
initialEnergy: number,
initialExperience: number,
energy: number[],
experience: number[],
): number {
let res = 0;
for (const v of experience) {
if (initialExperience <= v) {
res += v - initialExperience + 1;
initialExperience = v + 1;
}
initialExperience += v;
}
for (const v of energy) {
if (initialEnergy <= v) {
res += v - initialEnergy + 1;
initialEnergy = v + 1;
}
initialEnergy -= v;
}
return res;
}
function minNumberOfHours(
initialEnergy: number,
initialExperience: number,
energy: number[],
experience: number[],
): number {
const s = energy.reduce((a, b) => a + b, 0);
let ans = Math.max(0, s - initialEnergy + 1);
for (const x of experience) {
if (initialExperience <= x) {
ans += x - initialExperience + 1;
initialExperience = x + 1;
}
initialExperience += x;
}
return ans;
}