Skip to content

Add solution and test-cases for problem 2040 #1244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Loading