Skip to content

Commit fadde48

Browse files
committed
feat: add solutions to lc problem: No.2383
No.2383.Minimum Hours of Training to Win a Competition
1 parent 6e92b07 commit fadde48

File tree

3 files changed

+191
-1
lines changed

3 files changed

+191
-1
lines changed

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

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@
6767

6868
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 是对手的数量。
6969

70+
**方法二:贪心**
71+
72+
我们可以先在初始时,把精力直接补充到足够击败这 $n$ 个对手,因此初始训练小时数为 $ans = \max(0, (\sum_{i=0}^{n-1} energy[i]) - initialEnergy + 1)$。
73+
74+
接下来我们只需考虑经验值的问题。遍历 $n$ 个对手,若当前经验不足以超过对手,则将经验补到刚好能超过该对手,击败对手后,把对手的经验值加到自己身上。
75+
76+
遍历结束,返回训练的小时数。
77+
78+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 是对手的数量。
79+
7080
<!-- tabs:start -->
7181

7282
### **Python3**
@@ -95,6 +105,18 @@ class Solution:
95105
return ans
96106
```
97107

108+
```python
109+
class Solution:
110+
def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:
111+
ans = max(0, sum(energy) - initialEnergy + 1)
112+
for x in experience:
113+
if initialExperience <= x:
114+
ans += x - initialExperience + 1
115+
initialExperience = x + 1
116+
initialExperience += x
117+
return ans
118+
```
119+
98120
### **Java**
99121

100122
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -122,6 +144,26 @@ class Solution {
122144
}
123145
```
124146

147+
```java
148+
class Solution {
149+
public int minNumberOfHours(int initialEnergy, int initialExperience, int[] energy, int[] experience) {
150+
int s = 0;
151+
for (int x : energy) {
152+
s += x;
153+
}
154+
int ans = Math.max(0, s - initialEnergy + 1);
155+
for (int x : experience) {
156+
if (initialExperience <= x) {
157+
ans += x - initialExperience + 1;
158+
initialExperience = x + 1;
159+
}
160+
initialExperience += x;
161+
}
162+
return ans;
163+
}
164+
}
165+
```
166+
125167
### **C++**
126168

127169
```cpp
@@ -147,6 +189,24 @@ public:
147189
};
148190
```
149191
192+
```cpp
193+
class Solution {
194+
public:
195+
int minNumberOfHours(int initialEnergy, int initialExperience, vector<int>& energy, vector<int>& experience) {
196+
int s = accumulate(energy.begin(), energy.end(), 0);
197+
int ans = max(0, s - initialEnergy + 1);
198+
for (int x : experience) {
199+
if (initialExperience <= x) {
200+
ans += x - initialExperience + 1;
201+
initialExperience = x + 1;
202+
}
203+
initialExperience += x;
204+
}
205+
return ans;
206+
}
207+
};
208+
```
209+
150210
### **Go**
151211

152212
```go
@@ -169,6 +229,26 @@ func minNumberOfHours(initialEnergy int, initialExperience int, energy []int, ex
169229
}
170230
```
171231

232+
```go
233+
func minNumberOfHours(initialEnergy int, initialExperience int, energy []int, experience []int) (ans int) {
234+
s := 0
235+
for _, x := range energy {
236+
s += x
237+
}
238+
if y := s - initialEnergy + 1; y > 0 {
239+
ans = y
240+
}
241+
for _, x := range experience {
242+
if initialExperience <= x {
243+
ans += x - initialExperience + 1
244+
initialExperience = x + 1
245+
}
246+
initialExperience += x
247+
}
248+
return
249+
}
250+
```
251+
172252
### **TypeScript**
173253

174254
```ts
@@ -226,6 +306,26 @@ function minNumberOfHours(
226306
}
227307
```
228308

309+
```ts
310+
function minNumberOfHours(
311+
initialEnergy: number,
312+
initialExperience: number,
313+
energy: number[],
314+
experience: number[],
315+
): number {
316+
const s = energy.reduce((a, b) => a + b, 0);
317+
let ans = Math.max(0, s - initialEnergy + 1);
318+
for (const x of experience) {
319+
if (initialExperience <= x) {
320+
ans += x - initialExperience + 1;
321+
initialExperience = x + 1;
322+
}
323+
initialExperience += x;
324+
}
325+
return ans;
326+
}
327+
```
328+
229329
### **C**
230330

231331
```c

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

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ class Solution:
8181
return ans
8282
```
8383

84+
```python
85+
class Solution:
86+
def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:
87+
ans = max(0, sum(energy) - initialEnergy + 1)
88+
for x in experience:
89+
if initialExperience <= x:
90+
ans += x - initialExperience + 1
91+
initialExperience = x + 1
92+
initialExperience += x
93+
return ans
94+
```
95+
8496
### **Java**
8597

8698
```java
@@ -106,6 +118,26 @@ class Solution {
106118
}
107119
```
108120

121+
```java
122+
class Solution {
123+
public int minNumberOfHours(int initialEnergy, int initialExperience, int[] energy, int[] experience) {
124+
int s = 0;
125+
for (int x : energy) {
126+
s += x;
127+
}
128+
int ans = Math.max(0, s - initialEnergy + 1);
129+
for (int x : experience) {
130+
if (initialExperience <= x) {
131+
ans += x - initialExperience + 1;
132+
initialExperience = x + 1;
133+
}
134+
initialExperience += x;
135+
}
136+
return ans;
137+
}
138+
}
139+
```
140+
109141
### **C++**
110142

111143
```cpp
@@ -131,6 +163,24 @@ public:
131163
};
132164
```
133165
166+
```cpp
167+
class Solution {
168+
public:
169+
int minNumberOfHours(int initialEnergy, int initialExperience, vector<int>& energy, vector<int>& experience) {
170+
int s = accumulate(energy.begin(), energy.end(), 0);
171+
int ans = max(0, s - initialEnergy + 1);
172+
for (int x : experience) {
173+
if (initialExperience <= x) {
174+
ans += x - initialExperience + 1;
175+
initialExperience = x + 1;
176+
}
177+
initialExperience += x;
178+
}
179+
return ans;
180+
}
181+
};
182+
```
183+
134184
### **Go**
135185

136186
```go
@@ -153,6 +203,26 @@ func minNumberOfHours(initialEnergy int, initialExperience int, energy []int, ex
153203
}
154204
```
155205

206+
```go
207+
func minNumberOfHours(initialEnergy int, initialExperience int, energy []int, experience []int) (ans int) {
208+
s := 0
209+
for _, x := range energy {
210+
s += x
211+
}
212+
if y := s - initialEnergy + 1; y > 0 {
213+
ans = y
214+
}
215+
for _, x := range experience {
216+
if initialExperience <= x {
217+
ans += x - initialExperience + 1
218+
initialExperience = x + 1
219+
}
220+
initialExperience += x
221+
}
222+
return
223+
}
224+
```
225+
156226
### **TypeScript**
157227

158228
```ts
@@ -210,6 +280,26 @@ function minNumberOfHours(
210280
}
211281
```
212282

283+
```ts
284+
function minNumberOfHours(
285+
initialEnergy: number,
286+
initialExperience: number,
287+
energy: number[],
288+
experience: number[],
289+
): number {
290+
const s = energy.reduce((a, b) => a + b, 0);
291+
let ans = Math.max(0, s - initialEnergy + 1);
292+
for (const x of experience) {
293+
if (initialExperience <= x) {
294+
ans += x - initialExperience + 1;
295+
initialExperience = x + 1;
296+
}
297+
initialExperience += x;
298+
}
299+
return ans;
300+
}
301+
```
302+
213303
### **C**
214304

215305
```c

solution/2300-2399/2384.Largest Palindromic Number/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262

6363
我们去除回文串的前导零,若回文串为空,则返回“0”。否则返回该回文串。
6464

65-
时间复杂度 $O(n)$。
65+
时间复杂度 $O(n)$,其中 $n$ 为 $num$ 的长度
6666

6767
<!-- tabs:start -->
6868

0 commit comments

Comments
 (0)