Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.2383 (#3538)
Browse files Browse the repository at this point in the history
No.2383.Minimum Hours of Training to Win a Competition
  • Loading branch information
yanglbme authored Sep 19, 2024
1 parent ff04023 commit c238c6b
Show file tree
Hide file tree
Showing 15 changed files with 267 additions and 711 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
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;
int minNumberOfHours(int x, int y, int* energy, int energySize, int* experience, int experienceSize) {
int ans = 0;
for (int i = 0; i < energySize; ++i) {
int dx = energy[i], dy = experience[i];
if (x <= dx) {
ans += dx + 1 - x;
x = dx + 1;
}
if (initialExperience <= experience[i]) {
res += experience[i] - initialExperience + 1;
initialExperience = experience[i] + 1;
if (y <= dy) {
ans += dy + 1 - y;
y = dy + 1;
}
initialEnergy -= energy[i];
initialExperience += experience[i];
x -= dx;
y += dy;
}
return res;
}
return ans;
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
class Solution {
public:
int minNumberOfHours(int initialEnergy, int initialExperience, vector<int>& energy, vector<int>& experience) {
int minNumberOfHours(int x, int y, 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;
int dx = energy[i], dy = experience[i];
if (x <= dx) {
ans += dx + 1 - x;
x = dx + 1;
}
if (initialExperience <= b) {
ans += b - initialExperience + 1;
initialExperience = b + 1;
if (y <= dy) {
ans += dy + 1 - y;
y = dy + 1;
}
initialEnergy -= a;
initialExperience += b;
x -= dx;
y += dy;
}
return ans;
}
};
};
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
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
func minNumberOfHours(x int, y int, energy []int, experience []int) (ans int) {
for i, dx := range energy {
dy := experience[i]
if x <= dx {
ans += dx + 1 - x
x = dx + 1
}
if initialExperience <= b {
ans += b - initialExperience + 1
initialExperience = b + 1
if y <= dy {
ans += dy + 1 - y
y = dy + 1
}
initialEnergy -= a
initialExperience += b
x -= dx
y += dy
}
return ans
}
return
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
class Solution {
public int minNumberOfHours(
int initialEnergy, int initialExperience, int[] energy, int[] experience) {
public int minNumberOfHours(int x, int y, 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;
int dx = energy[i], dy = experience[i];
if (x <= dx) {
ans += dx + 1 - x;
x = dx + 1;
}
if (initialExperience <= b) {
ans += b - initialExperience + 1;
initialExperience = b + 1;
if (y <= dy) {
ans += dy + 1 - y;
y = dy + 1;
}
initialEnergy -= a;
initialExperience += b;
x -= dx;
y += dy;
}
return ans;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
class Solution:
def minNumberOfHours(
self,
initialEnergy: int,
initialExperience: int,
energy: List[int],
experience: List[int],
self, x: int, y: 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
for dx, dy in zip(energy, experience):
if x <= dx:
ans += dx + 1 - x
x = dx + 1
if y <= dy:
ans += dy + 1 - y
y = dy + 1
x -= dx
y += dy
return ans
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
impl Solution {
pub fn min_number_of_hours(
mut initial_energy: i32,
mut initial_experience: i32,
mut x: i32,
mut y: 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;
let mut ans = 0;

for (&dx, &dy) in energy.iter().zip(experience.iter()) {
if x <= dx {
ans += dx + 1 - x;
x = dx + 1;
}
if initial_experience <= experience[i] {
res += experience[i] - initial_experience + 1;
initial_experience = experience[i] + 1;
if y <= dy {
ans += dy + 1 - y;
y = dy + 1;
}
initial_energy -= energy[i];
initial_experience += experience[i];
x -= dx;
y += dy;
}
res

ans
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
function minNumberOfHours(
initialEnergy: number,
initialExperience: number,
energy: number[],
experience: number[],
): number {
const n = energy.length;
function minNumberOfHours(x: number, y: number, energy: number[], experience: number[]): number {
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;
for (let i = 0; i < energy.length; ++i) {
const [dx, dy] = [energy[i], experience[i]];
if (x <= dx) {
ans += dx + 1 - x;
x = dx + 1;
}
if (initialExperience <= minExperience) {
const need = minExperience - initialExperience + 1;
ans += need;
initialExperience += need;
if (y <= dy) {
ans += dy + 1 - y;
y = dy + 1;
}
initialEnergy -= minEnergy;
initialExperience += minExperience;
x -= dx;
y += dy;
}
return ans;
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit c238c6b

Please sign in to comment.