Skip to content

Commit 06516ce

Browse files
committed
feat: add solutions to lc problem: No.2604
No.2604.Minimum Time to Eat All Grains
1 parent b6fc338 commit 06516ce

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

solution/2600-2699/2604.Minimum Time to Eat All Grains/README.md

+52
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,58 @@ func abs(x int) int {
270270
}
271271
```
272272

273+
### **TypeScript**
274+
275+
```ts
276+
function minimumTime(hens: number[], grains: number[]): number {
277+
hens.sort((a, b) => a - b);
278+
grains.sort((a, b) => a - b);
279+
const m = grains.length;
280+
let l = 0;
281+
let r = Math.abs(hens[0] - grains[0]) + grains[m - 1] - grains[0] + 1;
282+
283+
const check = (t: number): boolean => {
284+
let j = 0;
285+
for (const x of hens) {
286+
if (j === m) {
287+
return true;
288+
}
289+
const y = grains[j];
290+
if (y <= x) {
291+
const d = x - y;
292+
if (d > t) {
293+
return false;
294+
}
295+
while (j < m && grains[j] <= x) {
296+
++j;
297+
}
298+
while (
299+
j < m &&
300+
Math.min(d, grains[j] - x) + grains[j] - y <= t
301+
) {
302+
++j;
303+
}
304+
} else {
305+
while (j < m && grains[j] - x <= t) {
306+
++j;
307+
}
308+
}
309+
}
310+
return j === m;
311+
};
312+
313+
while (l < r) {
314+
const mid = (l + r) >> 1;
315+
if (check(mid)) {
316+
r = mid;
317+
} else {
318+
l = mid + 1;
319+
}
320+
}
321+
return l;
322+
}
323+
```
324+
273325
### **...**
274326

275327
```

solution/2600-2699/2604.Minimum Time to Eat All Grains/README_EN.md

+52
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,58 @@ func abs(x int) int {
249249
}
250250
```
251251

252+
### **TypeScript**
253+
254+
```ts
255+
function minimumTime(hens: number[], grains: number[]): number {
256+
hens.sort((a, b) => a - b);
257+
grains.sort((a, b) => a - b);
258+
const m = grains.length;
259+
let l = 0;
260+
let r = Math.abs(hens[0] - grains[0]) + grains[m - 1] - grains[0] + 1;
261+
262+
const check = (t: number): boolean => {
263+
let j = 0;
264+
for (const x of hens) {
265+
if (j === m) {
266+
return true;
267+
}
268+
const y = grains[j];
269+
if (y <= x) {
270+
const d = x - y;
271+
if (d > t) {
272+
return false;
273+
}
274+
while (j < m && grains[j] <= x) {
275+
++j;
276+
}
277+
while (
278+
j < m &&
279+
Math.min(d, grains[j] - x) + grains[j] - y <= t
280+
) {
281+
++j;
282+
}
283+
} else {
284+
while (j < m && grains[j] - x <= t) {
285+
++j;
286+
}
287+
}
288+
}
289+
return j === m;
290+
};
291+
292+
while (l < r) {
293+
const mid = (l + r) >> 1;
294+
if (check(mid)) {
295+
r = mid;
296+
} else {
297+
l = mid + 1;
298+
}
299+
}
300+
return l;
301+
}
302+
```
303+
252304
### **...**
253305

254306
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
function minimumTime(hens: number[], grains: number[]): number {
2+
hens.sort((a, b) => a - b);
3+
grains.sort((a, b) => a - b);
4+
const m = grains.length;
5+
let l = 0;
6+
let r = Math.abs(hens[0] - grains[0]) + grains[m - 1] - grains[0] + 1;
7+
8+
const check = (t: number): boolean => {
9+
let j = 0;
10+
for (const x of hens) {
11+
if (j === m) {
12+
return true;
13+
}
14+
const y = grains[j];
15+
if (y <= x) {
16+
const d = x - y;
17+
if (d > t) {
18+
return false;
19+
}
20+
while (j < m && grains[j] <= x) {
21+
++j;
22+
}
23+
while (
24+
j < m &&
25+
Math.min(d, grains[j] - x) + grains[j] - y <= t
26+
) {
27+
++j;
28+
}
29+
} else {
30+
while (j < m && grains[j] - x <= t) {
31+
++j;
32+
}
33+
}
34+
}
35+
return j === m;
36+
};
37+
38+
while (l < r) {
39+
const mid = (l + r) >> 1;
40+
if (check(mid)) {
41+
r = mid;
42+
} else {
43+
l = mid + 1;
44+
}
45+
}
46+
return l;
47+
}

0 commit comments

Comments
 (0)