Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7ad2c64

Browse files
committed
Add solution #2040
1 parent fb018a3 commit 7ad2c64

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,702 LeetCode solutions in JavaScript
1+
# 1,703 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1564,6 +1564,7 @@
15641564
2037|[Minimum Number of Moves to Seat Everyone](./solutions/2037-minimum-number-of-moves-to-seat-everyone.js)|Easy|
15651565
2038|[Remove Colored Pieces if Both Neighbors are the Same Color](./solutions/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color.js)|Medium|
15661566
2039|[The Time When the Network Becomes Idle](./solutions/2039-the-time-when-the-network-becomes-idle.js)|Medium|
1567+
2040|[Kth Smallest Product of Two Sorted Arrays](./solutions/2040-kth-smallest-product-of-two-sorted-arrays.js)|Hard|
15671568
2047|[Number of Valid Words in a Sentence](./solutions/2047-number-of-valid-words-in-a-sentence.js)|Easy|
15681569
2053|[Kth Distinct String in an Array](./solutions/2053-kth-distinct-string-in-an-array.js)|Medium|
15691570
2071|[Maximum Number of Tasks You Can Assign](./solutions/2071-maximum-number-of-tasks-you-can-assign.js)|Hard|
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* 2040. Kth Smallest Product of Two Sorted Arrays
3+
* https://leetcode.com/problems/kth-smallest-product-of-two-sorted-arrays/
4+
* Difficulty: Hard
5+
*
6+
* Given two sorted 0-indexed integer arrays nums1 and nums2 as well as an integer k, return
7+
* the kth (1-based) smallest product of nums1[i] * nums2[j] where 0 <= i < nums1.length
8+
* and 0 <= j < nums2.length.
9+
*/
10+
11+
/**
12+
* @param {number[]} nums1
13+
* @param {number[]} nums2
14+
* @param {number} k
15+
* @return {number}
16+
*/
17+
var kthSmallestProduct = function(nums1, nums2, k) {
18+
function countProducts(maxProduct) {
19+
let count = 0;
20+
for (const num1 of nums1) {
21+
let left = 0;
22+
let right = nums2.length;
23+
24+
if (num1 >= 0) {
25+
while (left < right) {
26+
const mid = Math.floor((left + right) / 2);
27+
if (num1 * nums2[mid] <= maxProduct) left = mid + 1;
28+
else right = mid;
29+
}
30+
count += left;
31+
} else {
32+
while (left < right) {
33+
const mid = Math.floor((left + right) / 2);
34+
if (num1 * nums2[mid] <= maxProduct) right = mid;
35+
else left = mid + 1;
36+
}
37+
count += nums2.length - left;
38+
}
39+
}
40+
return count;
41+
}
42+
43+
let low = -(10 ** 10);
44+
let high = 10 ** 10;
45+
46+
while (low < high) {
47+
const mid = Math.floor((low + high) / 2);
48+
if (countProducts(mid) >= k) high = mid;
49+
else low = mid + 1;
50+
}
51+
52+
return low;
53+
};

0 commit comments

Comments
 (0)