返回介绍

solution / 2300-2399 / 2383.Minimum Hours of Training to Win a Competition / README_EN

发布于 2024-06-17 01:03:07 字数 11046 浏览 0 评论 0 收藏 0

2383. Minimum Hours of Training to Win a Competition

中文文档

Description

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

 

Example 1:

Input: initialEnergy = 5, initialExperience = 3, energy = [1,4,3,2], experience = [2,6,3,1]
Output: 8
Explanation: You can increase your energy to 11 after 6 hours of training, and your experience to 5 after 2 hours of training.
You face the opponents in the following order:
- You have more energy and experience than the 0th opponent so you win.
  Your energy becomes 11 - 1 = 10, and your experience becomes 5 + 2 = 7.
- You have more energy and experience than the 1st opponent so you win.
  Your energy becomes 10 - 4 = 6, and your experience becomes 7 + 6 = 13.
- You have more energy and experience than the 2nd opponent so you win.
  Your energy becomes 6 - 3 = 3, and your experience becomes 13 + 3 = 16.
- You have more energy and experience than the 3rd opponent so you win.
  Your energy becomes 3 - 2 = 1, and your experience becomes 16 + 1 = 17.
You did a total of 6 + 2 = 8 hours of training before the competition, so we return 8.
It can be proven that no smaller answer exists.

Example 2:

Input: initialEnergy = 2, initialExperience = 4, energy = [1], experience = [3]
Output: 0
Explanation: You do not need any additional energy or experience to win the competition, so we return 0.

 

Constraints:

  • n == energy.length == experience.length
  • 1 <= n <= 100
  • 1 <= initialEnergy, initialExperience, energy[i], experience[i] <= 100

Solutions

Solution 1

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;
}

Solution 2

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;
}

Solution 3

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文