Skip to content

Commit 98a01ce

Browse files
authored
Merge pull request #1244 from 0xff-dev/2040
Add solution and test-cases for problem 2040
2 parents 51f36c0 + 7746864 commit 98a01ce

File tree

3 files changed

+93
-25
lines changed

3 files changed

+93
-25
lines changed

leetcode/2001-2100/2040.Kth-Smallest-Product-of-Two-Sorted-Arrays/README.md

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
11
# [2040.Kth Smallest Product of Two Sorted Arrays][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
74

5+
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`.
6+
87
**Example 1:**
98

109
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
10+
Input: nums1 = [2,5], nums2 = [3,4], k = 2
11+
Output: 8
12+
Explanation: The 2 smallest products are:
13+
- nums1[0] * nums2[0] = 2 * 3 = 6
14+
- nums1[0] * nums2[1] = 2 * 4 = 8
15+
The 2nd smallest product is 8.
1316
```
1417

15-
## 题意
16-
> ...
18+
**Example 2:**
1719

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Kth Smallest Product of Two Sorted Arrays
23-
```go
20+
```
21+
Input: nums1 = [-4,-2,0,3], nums2 = [2,4], k = 6
22+
Output: 0
23+
Explanation: The 6 smallest products are:
24+
- nums1[0] * nums2[1] = (-4) * 4 = -16
25+
- nums1[0] * nums2[0] = (-4) * 2 = -8
26+
- nums1[1] * nums2[1] = (-2) * 4 = -8
27+
- nums1[1] * nums2[0] = (-2) * 2 = -4
28+
- nums1[2] * nums2[0] = 0 * 2 = 0
29+
- nums1[2] * nums2[1] = 0 * 4 = 0
30+
The 6th smallest product is 0.
2431
```
2532

33+
**Example 3:**
34+
35+
```
36+
Input: nums1 = [-2,-1,0,1,2], nums2 = [-3,-1,2,4,5], k = 3
37+
Output: -6
38+
Explanation: The 3 smallest products are:
39+
- nums1[0] * nums2[4] = (-2) * 5 = -10
40+
- nums1[0] * nums2[3] = (-2) * 4 = -8
41+
- nums1[4] * nums2[0] = 2 * (-3) = -6
42+
The 3rd smallest product is -6.
43+
```
2644

2745
## 结语
2846

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,55 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import (
4+
"math"
5+
"sort"
6+
)
7+
8+
func Solution(nums1, nums2 []int, k int64) int64 {
9+
// 保证nums1更短,减少遍历次数
10+
if len(nums1) > len(nums2) {
11+
return Solution(nums2, nums1, k)
12+
}
13+
14+
left, right := int64(-1e10), int64(1e10)
15+
for left < right {
16+
mid := left + (right-left)/2
17+
count := checkNums(mid, nums1, nums2)
18+
if count >= k {
19+
right = mid
20+
} else {
21+
left = mid + 1
22+
}
23+
}
24+
return left
25+
}
26+
27+
func checkNums(mid int64, nums1, nums2 []int) int64 {
28+
var ret int64 = 0
29+
for _, x := range nums1 {
30+
if x == 0 {
31+
if mid >= 0 {
32+
ret += int64(len(nums2))
33+
}
34+
// 如果mid<0,乘积都>=0,不计入
35+
} else if x > 0 {
36+
// 计算上界:mid / x
37+
yy := int64(math.Floor(float64(mid) / float64(x)))
38+
// upper_bound
39+
idx := sort.Search(len(nums2), func(i int) bool {
40+
return int64(nums2[i]) > yy
41+
})
42+
ret += int64(idx)
43+
} else {
44+
// x < 0
45+
// 计算下界:ceil(mid / x)
46+
yy := int64(math.Ceil(float64(mid) / float64(x)))
47+
// lower_bound
48+
idx := sort.Search(len(nums2), func(i int) bool {
49+
return int64(nums2[i]) >= yy
50+
})
51+
ret += int64(len(nums2) - idx)
52+
}
53+
}
54+
return ret
555
}

leetcode/2001-2100/2040.Kth-Smallest-Product-of-Two-Sorted-Arrays/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
n1, n2 []int
14+
k int64
15+
expect int64
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{2, 5}, []int{3, 4}, 2, 8},
18+
{"TestCase2", []int{-2, -1, 0, 1, 2}, []int{-3, -1, 2, 4, 5}, 3, -6},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.n1, c.n2, c.k)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
27+
c.expect, got, c.n1, c.n2, c.k)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)