From 7746864b9f7d384cd4fe75c6ddba6dcbd9944ae0 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Wed, 25 Jun 2025 09:14:14 +0800 Subject: [PATCH] Add solution and test-cases for problem 2040 --- .../README.md | 44 ++++++++++----- .../Solution.go | 54 ++++++++++++++++++- .../Solution_test.go | 20 +++---- 3 files changed, 93 insertions(+), 25 deletions(-) diff --git a/leetcode/2001-2100/2040.Kth-Smallest-Product-of-Two-Sorted-Arrays/README.md b/leetcode/2001-2100/2040.Kth-Smallest-Product-of-Two-Sorted-Arrays/README.md index 08c6a8fb2..ca0f4e8b3 100755 --- a/leetcode/2001-2100/2040.Kth-Smallest-Product-of-Two-Sorted-Arrays/README.md +++ b/leetcode/2001-2100/2040.Kth-Smallest-Product-of-Two-Sorted-Arrays/README.md @@ -1,28 +1,46 @@ # [2040.Kth Smallest Product of Two Sorted Arrays][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +Given two **sorted 0-indexed** integer arrays `nums1` and `nums2` as well as an integer `k`, return the `kth` **(1-based)** smallest product of `nums1[i] * nums2[j]` where `0 <= i < nums1.length` and `0 <= j < nums2.length`. + **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums1 = [2,5], nums2 = [3,4], k = 2 +Output: 8 +Explanation: The 2 smallest products are: +- nums1[0] * nums2[0] = 2 * 3 = 6 +- nums1[0] * nums2[1] = 2 * 4 = 8 +The 2nd smallest product is 8. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Kth Smallest Product of Two Sorted Arrays -```go +``` +Input: nums1 = [-4,-2,0,3], nums2 = [2,4], k = 6 +Output: 0 +Explanation: The 6 smallest products are: +- nums1[0] * nums2[1] = (-4) * 4 = -16 +- nums1[0] * nums2[0] = (-4) * 2 = -8 +- nums1[1] * nums2[1] = (-2) * 4 = -8 +- nums1[1] * nums2[0] = (-2) * 2 = -4 +- nums1[2] * nums2[0] = 0 * 2 = 0 +- nums1[2] * nums2[1] = 0 * 4 = 0 +The 6th smallest product is 0. ``` +**Example 3:** + +``` +Input: nums1 = [-2,-1,0,1,2], nums2 = [-3,-1,2,4,5], k = 3 +Output: -6 +Explanation: The 3 smallest products are: +- nums1[0] * nums2[4] = (-2) * 5 = -10 +- nums1[0] * nums2[3] = (-2) * 4 = -8 +- nums1[4] * nums2[0] = 2 * (-3) = -6 +The 3rd smallest product is -6. +``` ## 结语 diff --git a/leetcode/2001-2100/2040.Kth-Smallest-Product-of-Two-Sorted-Arrays/Solution.go b/leetcode/2001-2100/2040.Kth-Smallest-Product-of-Two-Sorted-Arrays/Solution.go index d115ccf5e..5c8c5421c 100644 --- a/leetcode/2001-2100/2040.Kth-Smallest-Product-of-Two-Sorted-Arrays/Solution.go +++ b/leetcode/2001-2100/2040.Kth-Smallest-Product-of-Two-Sorted-Arrays/Solution.go @@ -1,5 +1,55 @@ package Solution -func Solution(x bool) bool { - return x +import ( + "math" + "sort" +) + +func Solution(nums1, nums2 []int, k int64) int64 { + // 保证nums1更短,减少遍历次数 + if len(nums1) > len(nums2) { + return Solution(nums2, nums1, k) + } + + left, right := int64(-1e10), int64(1e10) + for left < right { + mid := left + (right-left)/2 + count := checkNums(mid, nums1, nums2) + if count >= k { + right = mid + } else { + left = mid + 1 + } + } + return left +} + +func checkNums(mid int64, nums1, nums2 []int) int64 { + var ret int64 = 0 + for _, x := range nums1 { + if x == 0 { + if mid >= 0 { + ret += int64(len(nums2)) + } + // 如果mid<0,乘积都>=0,不计入 + } else if x > 0 { + // 计算上界:mid / x + yy := int64(math.Floor(float64(mid) / float64(x))) + // upper_bound + idx := sort.Search(len(nums2), func(i int) bool { + return int64(nums2[i]) > yy + }) + ret += int64(idx) + } else { + // x < 0 + // 计算下界:ceil(mid / x) + yy := int64(math.Ceil(float64(mid) / float64(x))) + // lower_bound + idx := sort.Search(len(nums2), func(i int) bool { + return int64(nums2[i]) >= yy + }) + ret += int64(len(nums2) - idx) + } + } + return ret } diff --git a/leetcode/2001-2100/2040.Kth-Smallest-Product-of-Two-Sorted-Arrays/Solution_test.go b/leetcode/2001-2100/2040.Kth-Smallest-Product-of-Two-Sorted-Arrays/Solution_test.go index 14ff50eb4..80c862416 100644 --- a/leetcode/2001-2100/2040.Kth-Smallest-Product-of-Two-Sorted-Arrays/Solution_test.go +++ b/leetcode/2001-2100/2040.Kth-Smallest-Product-of-Two-Sorted-Arrays/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + n1, n2 []int + k int64 + expect int64 }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{2, 5}, []int{3, 4}, 2, 8}, + {"TestCase2", []int{-2, -1, 0, 1, 2}, []int{-3, -1, 2, 4, 5}, 3, -6}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.n1, c.n2, c.k) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v", + c.expect, got, c.n1, c.n2, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }