-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.2383 (#3538)
No.2383.Minimum Hours of Training to Win a Competition
- Loading branch information
Showing
15 changed files
with
267 additions
and
711 deletions.
There are no files selected for viewing
348 changes: 89 additions & 259 deletions
348
solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/README.md
Large diffs are not rendered by default.
Oops, something went wrong.
342 changes: 93 additions & 249 deletions
342
...tion/2300-2399/2383.Minimum Hours of Training to Win a Competition/README_EN.md
Large diffs are not rendered by default.
Oops, something went wrong.
27 changes: 14 additions & 13 deletions
27
solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/Solution.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
22 changes: 11 additions & 11 deletions
22
solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; | ||
}; |
27 changes: 13 additions & 14 deletions
27
solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
23 changes: 11 additions & 12 deletions
23
solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
24 changes: 10 additions & 14 deletions
24
solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
29 changes: 15 additions & 14 deletions
29
solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/Solution.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
31 changes: 11 additions & 20 deletions
31
solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/Solution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
15 changes: 0 additions & 15 deletions
15
solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/Solution2.cpp
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/Solution2.go
This file was deleted.
Oops, something went wrong.
18 changes: 0 additions & 18 deletions
18
solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/Solution2.java
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/Solution2.py
This file was deleted.
Oops, something went wrong.
23 changes: 0 additions & 23 deletions
23
solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/Solution2.ts
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/Solution3.ts
This file was deleted.
Oops, something went wrong.