Skip to content

Commit

Permalink
Merge pull request #957 from 0xff-dev/719
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 719
  • Loading branch information
6boris authored Sep 9, 2024
2 parents 5768900 + a1a7e3e commit 1a7c2c6
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 25 deletions.
33 changes: 20 additions & 13 deletions leetcode/701-800/0719.Find-K-th-Smallest-Pair-Distance/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
# [719.Find K-th Smallest Pair Distance][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
The **distance of a pair** of integers `a` and `b` is defined as the absolute difference between `a` and `b`.

Given an integer array `nums` and an integer `k`, return the `kth` smallest **distance among all the pairs** `nums[i]` and `nums[j]` where `0 <= i < j < nums.length`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: nums = [1,3,1], k = 1
Output: 0
Explanation: Here are all the pairs:
(1,3) -> 2
(1,1) -> 0
(3,1) -> 2
Then the 1st smallest distance pair is (1,1), and its distance is 0.
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Find K-th Smallest Pair Distance
```go
```
Input: nums = [1,1,1], k = 2
Output: 0
```

**Example 3:**

```
Input: nums = [1,6,1], k = 3
Output: 5
```

## 结语

Expand Down
40 changes: 38 additions & 2 deletions leetcode/701-800/0719.Find-K-th-Smallest-Pair-Distance/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
package Solution

func Solution(x bool) bool {
return x
import "sort"

func Solution(nums []int, k int) int {
sort.Ints(nums)
// 通常情况下 那基本就是n^2 + nlog(n)了,计算出全部,然后重新排列
// 对于条件i < j, 就是选择(i, j)或者(j, i)的问题, 可以直接忽略了
// 如果完全计算,肯定是不行的,10^8 如果选择最后几个元素,内存都要爆炸
l := len(nums)
var count func(int) int
count = func(target int) int {
ans := 0
// 4, 62, 100
for i := 1; i < l; i++ {
idx := sort.Search(i, func(ii int) bool {
return nums[i]-nums[ii] <= target
})
ans += i - idx
}
return ans
}
a, b := 0, nums[l-1]-nums[0]
// 查看小于等于当前值的有多少个
// 4, 62, 100
// 38,58,96,
// 0, 5, 5
// 0 ~ 5
// 3 ~ 5
// 4 ~ 5
for a < b {
mid := (a + b) / 2
c := count(mid)
if c < k {
a = mid + 1
continue
}
b = mid
}
return a
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
nums []int
k int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 3, 1}, 1, 0},
{"TestCase2", []int{1, 1, 1}, 2, 0},
{"TestCase3", []int{1, 6, 1}, 3, 5},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.nums, 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",
c.expect, got, c.nums, c.k)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit 1a7c2c6

Please sign in to comment.