File tree Expand file tree Collapse file tree 3 files changed +184
-0
lines changed
solution/2000-2099/2040.Kth Smallest Product of Two Sorted Arrays Expand file tree Collapse file tree 3 files changed +184
-0
lines changed Original file line number Diff line number Diff line change @@ -293,6 +293,69 @@ func abs(x int) int {
293
293
}
294
294
```
295
295
296
+ #### TypeScript
297
+
298
+ ``` ts
299
+ function kthSmallestProduct(nums1 : number [], nums2 : number [], k : number ): number {
300
+ const m = nums1 .length ;
301
+ const n = nums2 .length ;
302
+
303
+ const a = BigInt (Math .max (Math .abs (nums1 [0 ]), Math .abs (nums1 [m - 1 ])));
304
+ const b = BigInt (Math .max (Math .abs (nums2 [0 ]), Math .abs (nums2 [n - 1 ])));
305
+
306
+ let l = - a * b ;
307
+ let r = a * b ;
308
+
309
+ const count = (p : bigint ): bigint => {
310
+ let cnt = 0n ;
311
+ for (const x of nums1 ) {
312
+ const bx = BigInt (x );
313
+ if (bx > 0n ) {
314
+ let l = 0 ,
315
+ r = n ;
316
+ while (l < r ) {
317
+ const mid = (l + r ) >> 1 ;
318
+ const prod = bx * BigInt (nums2 [mid ]);
319
+ if (prod > p ) {
320
+ r = mid ;
321
+ } else {
322
+ l = mid + 1 ;
323
+ }
324
+ }
325
+ cnt += BigInt (l );
326
+ } else if (bx < 0n ) {
327
+ let l = 0 ,
328
+ r = n ;
329
+ while (l < r ) {
330
+ const mid = (l + r ) >> 1 ;
331
+ const prod = bx * BigInt (nums2 [mid ]);
332
+ if (prod <= p ) {
333
+ r = mid ;
334
+ } else {
335
+ l = mid + 1 ;
336
+ }
337
+ }
338
+ cnt += BigInt (n - l );
339
+ } else if (p >= 0n ) {
340
+ cnt += BigInt (n );
341
+ }
342
+ }
343
+ return cnt ;
344
+ };
345
+
346
+ while (l < r ) {
347
+ const mid = (l + r ) >> 1n ;
348
+ if (count (mid ) >= BigInt (k )) {
349
+ r = mid ;
350
+ } else {
351
+ l = mid + 1n ;
352
+ }
353
+ }
354
+
355
+ return Number (l );
356
+ }
357
+ ```
358
+
296
359
<!-- tabs: end -->
297
360
298
361
<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -294,6 +294,69 @@ func abs(x int) int {
294
294
}
295
295
```
296
296
297
+ #### TypeScript
298
+
299
+ ``` ts
300
+ function kthSmallestProduct(nums1 : number [], nums2 : number [], k : number ): number {
301
+ const m = nums1 .length ;
302
+ const n = nums2 .length ;
303
+
304
+ const a = BigInt (Math .max (Math .abs (nums1 [0 ]), Math .abs (nums1 [m - 1 ])));
305
+ const b = BigInt (Math .max (Math .abs (nums2 [0 ]), Math .abs (nums2 [n - 1 ])));
306
+
307
+ let l = - a * b ;
308
+ let r = a * b ;
309
+
310
+ const count = (p : bigint ): bigint => {
311
+ let cnt = 0n ;
312
+ for (const x of nums1 ) {
313
+ const bx = BigInt (x );
314
+ if (bx > 0n ) {
315
+ let l = 0 ,
316
+ r = n ;
317
+ while (l < r ) {
318
+ const mid = (l + r ) >> 1 ;
319
+ const prod = bx * BigInt (nums2 [mid ]);
320
+ if (prod > p ) {
321
+ r = mid ;
322
+ } else {
323
+ l = mid + 1 ;
324
+ }
325
+ }
326
+ cnt += BigInt (l );
327
+ } else if (bx < 0n ) {
328
+ let l = 0 ,
329
+ r = n ;
330
+ while (l < r ) {
331
+ const mid = (l + r ) >> 1 ;
332
+ const prod = bx * BigInt (nums2 [mid ]);
333
+ if (prod <= p ) {
334
+ r = mid ;
335
+ } else {
336
+ l = mid + 1 ;
337
+ }
338
+ }
339
+ cnt += BigInt (n - l );
340
+ } else if (p >= 0n ) {
341
+ cnt += BigInt (n );
342
+ }
343
+ }
344
+ return cnt ;
345
+ };
346
+
347
+ while (l < r ) {
348
+ const mid = (l + r ) >> 1n ;
349
+ if (count (mid ) >= BigInt (k )) {
350
+ r = mid ;
351
+ } else {
352
+ l = mid + 1n ;
353
+ }
354
+ }
355
+
356
+ return Number (l );
357
+ }
358
+ ```
359
+
297
360
<!-- tabs: end -->
298
361
299
362
<!-- solution: end -->
Original file line number Diff line number Diff line change
1
+ function kthSmallestProduct ( nums1 : number [ ] , nums2 : number [ ] , k : number ) : number {
2
+ const m = nums1 . length ;
3
+ const n = nums2 . length ;
4
+
5
+ const a = BigInt ( Math . max ( Math . abs ( nums1 [ 0 ] ) , Math . abs ( nums1 [ m - 1 ] ) ) ) ;
6
+ const b = BigInt ( Math . max ( Math . abs ( nums2 [ 0 ] ) , Math . abs ( nums2 [ n - 1 ] ) ) ) ;
7
+
8
+ let l = - a * b ;
9
+ let r = a * b ;
10
+
11
+ const count = ( p : bigint ) : bigint => {
12
+ let cnt = 0n ;
13
+ for ( const x of nums1 ) {
14
+ const bx = BigInt ( x ) ;
15
+ if ( bx > 0n ) {
16
+ let l = 0 ,
17
+ r = n ;
18
+ while ( l < r ) {
19
+ const mid = ( l + r ) >> 1 ;
20
+ const prod = bx * BigInt ( nums2 [ mid ] ) ;
21
+ if ( prod > p ) {
22
+ r = mid ;
23
+ } else {
24
+ l = mid + 1 ;
25
+ }
26
+ }
27
+ cnt += BigInt ( l ) ;
28
+ } else if ( bx < 0n ) {
29
+ let l = 0 ,
30
+ r = n ;
31
+ while ( l < r ) {
32
+ const mid = ( l + r ) >> 1 ;
33
+ const prod = bx * BigInt ( nums2 [ mid ] ) ;
34
+ if ( prod <= p ) {
35
+ r = mid ;
36
+ } else {
37
+ l = mid + 1 ;
38
+ }
39
+ }
40
+ cnt += BigInt ( n - l ) ;
41
+ } else if ( p >= 0n ) {
42
+ cnt += BigInt ( n ) ;
43
+ }
44
+ }
45
+ return cnt ;
46
+ } ;
47
+
48
+ while ( l < r ) {
49
+ const mid = ( l + r ) >> 1n ;
50
+ if ( count ( mid ) >= BigInt ( k ) ) {
51
+ r = mid ;
52
+ } else {
53
+ l = mid + 1n ;
54
+ }
55
+ }
56
+
57
+ return Number ( l ) ;
58
+ }
You can’t perform that action at this time.
0 commit comments