File tree 3 files changed +151
-0
lines changed
solution/2600-2699/2604.Minimum Time to Eat All Grains
3 files changed +151
-0
lines changed Original file line number Diff line number Diff line change @@ -270,6 +270,58 @@ func abs(x int) int {
270
270
}
271
271
```
272
272
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
+
273
325
### ** ...**
274
326
275
327
```
Original file line number Diff line number Diff line change @@ -249,6 +249,58 @@ func abs(x int) int {
249
249
}
250
250
```
251
251
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
+
252
304
### ** ...**
253
305
254
306
```
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments