Class Solution
java.lang.Object
g2001_2100.s2040_kth_smallest_product_of_two_sorted_arrays.Solution
2040 - Kth Smallest Product of Two Sorted Arrays.<p>Hard</p>
<p>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 <= i < nums1.length</code> <em>and</em> <code>0 <= j < nums2.length</code>.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> nums1 = [2,5], nums2 = [3,4], k = 2</p>
<p><strong>Output:</strong> 8</p>
<p><strong>Explanation:</strong> The 2 smallest products are:</p>
<ul>
<li>
<p>nums1[0] * nums2[0] = 2 * 3 = 6</p>
</li>
<li>
<p>nums1[0] * nums2[1] = 2 * 4 = 8</p>
</li>
</ul>
<p>The 2<sup>nd</sup> smallest product is 8.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> nums1 = [-4,-2,0,3], nums2 = [2,4], k = 6</p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong> The 6 smallest products are:</p>
<ul>
<li>
<p>nums1[0] * nums2[1] = (-4) * 4 = -16</p>
</li>
<li>
<p>nums1[0] * nums2[0] = (-4) * 2 = -8</p>
</li>
<li>
<p>nums1[1] * nums2[1] = (-2) * 4 = -8</p>
</li>
<li>
<p>nums1[1] * nums2[0] = (-2) * 2 = -4</p>
</li>
<li>
<p>nums1[2] * nums2[0] = 0 * 2 = 0</p>
</li>
<li>
<p>nums1[2] * nums2[1] = 0 * 4 = 0</p>
</li>
</ul>
<p>The 6<sup>th</sup> smallest product is 0.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> nums1 = [-2,-1,0,1,2], nums2 = [-3,-1,2,4,5], k = 3</p>
<p><strong>Output:</strong> -6</p>
<p><strong>Explanation:</strong> The 3 smallest products are:</p>
<ul>
<li>
<p>nums1[0] * nums2[4] = (-2) * 5 = -10</p>
</li>
<li>
<p>nums1[0] * nums2[3] = (-2) * 4 = -8</p>
</li>
<li>
<p>nums1[4] * nums2[0] = 2 * (-3) = -6</p>
</li>
</ul>
<p>The 3<sup>rd</sup> smallest product is -6.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length, nums2.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>5</sup> <= nums1[i], nums2[j] <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= nums1.length * nums2.length</code></li>
<li><code>nums1</code> and <code>nums2</code> are sorted.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
kthSmallestProduct
public long kthSmallestProduct(int[] nums1, int[] nums2, long k)
-