Skip to content

Commit 9a8b94b

Browse files
committed
Sync LeetCode submission Runtime - 3790 ms (25.00%), Memory - 25.8 MB (68.50%)
1 parent c32b063 commit 9a8b94b

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
Given two <strong>sorted 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> as well as an integer <code>k</code>, return <em>the </em><code>k<sup>th</sup></code><em> (<strong>1-based</strong>) smallest product of </em><code>nums1[i] * nums2[j]</code><em> where </em><code>0 &lt;= i &lt; nums1.length</code><em> and </em><code>0 &lt;= j &lt; nums2.length</code>.
2+
<p>&nbsp;</p>
3+
<p><strong class="example">Example 1:</strong></p>
4+
5+
<pre>
6+
<strong>Input:</strong> nums1 = [2,5], nums2 = [3,4], k = 2
7+
<strong>Output:</strong> 8
8+
<strong>Explanation:</strong> The 2 smallest products are:
9+
- nums1[0] * nums2[0] = 2 * 3 = 6
10+
- nums1[0] * nums2[1] = 2 * 4 = 8
11+
The 2<sup>nd</sup> smallest product is 8.
12+
</pre>
13+
14+
<p><strong class="example">Example 2:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> nums1 = [-4,-2,0,3], nums2 = [2,4], k = 6
18+
<strong>Output:</strong> 0
19+
<strong>Explanation:</strong> The 6 smallest products are:
20+
- nums1[0] * nums2[1] = (-4) * 4 = -16
21+
- nums1[0] * nums2[0] = (-4) * 2 = -8
22+
- nums1[1] * nums2[1] = (-2) * 4 = -8
23+
- nums1[1] * nums2[0] = (-2) * 2 = -4
24+
- nums1[2] * nums2[0] = 0 * 2 = 0
25+
- nums1[2] * nums2[1] = 0 * 4 = 0
26+
The 6<sup>th</sup> smallest product is 0.
27+
</pre>
28+
29+
<p><strong class="example">Example 3:</strong></p>
30+
31+
<pre>
32+
<strong>Input:</strong> nums1 = [-2,-1,0,1,2], nums2 = [-3,-1,2,4,5], k = 3
33+
<strong>Output:</strong> -6
34+
<strong>Explanation:</strong> The 3 smallest products are:
35+
- nums1[0] * nums2[4] = (-2) * 5 = -10
36+
- nums1[0] * nums2[3] = (-2) * 4 = -8
37+
- nums1[4] * nums2[0] = 2 * (-3) = -6
38+
The 3<sup>rd</sup> smallest product is -6.
39+
</pre>
40+
41+
<p>&nbsp;</p>
42+
<p><strong>Constraints:</strong></p>
43+
44+
<ul>
45+
<li><code>1 &lt;= nums1.length, nums2.length &lt;= 5 * 10<sup>4</sup></code></li>
46+
<li><code>-10<sup>5</sup> &lt;= nums1[i], nums2[j] &lt;= 10<sup>5</sup></code></li>
47+
<li><code>1 &lt;= k &lt;= nums1.length * nums2.length</code></li>
48+
<li><code>nums1</code> and <code>nums2</code> are sorted.</li>
49+
</ul>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def f(self, nums2: List[int], x1: int, v: int) -> int:
3+
if x1 > 0:
4+
return bisect_right(nums2, v // x1)
5+
elif x1 < 0:
6+
return len(nums2) - bisect_left(nums2, -(-v // x1))
7+
else:
8+
return len(nums2) if v >= 0 else 0
9+
10+
def kthSmallestProduct(
11+
self, nums1: List[int], nums2: List[int], k: int
12+
) -> int:
13+
n1 = len(nums1)
14+
left, right = -(10**10), 10**10
15+
while left <= right:
16+
mid = (left + right) // 2
17+
count = 0
18+
for i in range(n1):
19+
count += self.f(nums2, nums1[i], mid)
20+
if count < k:
21+
left = mid + 1
22+
else:
23+
right = mid - 1
24+
return left

0 commit comments

Comments
 (0)