Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2976139

Browse files
feat: add solutions to lc problem: No.2040 (doocs#4487)
No.2040.Kth Smallest Product of Two Sorted Arrays
1 parent 2d63f55 commit 2976139

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed

‎solution/2000-2099/2040.Kth Smallest Product of Two Sorted Arrays/README.md‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,69 @@ func abs(x int) int {
293293
}
294294
```
295295

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+
296359
<!-- tabs:end -->
297360

298361
<!-- solution:end -->

‎solution/2000-2099/2040.Kth Smallest Product of Two Sorted Arrays/README_EN.md‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,69 @@ func abs(x int) int {
294294
}
295295
```
296296

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+
297360
<!-- tabs:end -->
298361

299362
<!-- solution:end -->
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /