From a1a7e3e8e71773c2220c94df44109e73276613d8 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Wed, 14 Aug 2024 22:48:25 +0800 Subject: [PATCH] Add solution and test-cases for problem 719 --- .../README.md | 33 +++++++++------ .../Solution.go | 40 ++++++++++++++++++- .../Solution_test.go | 21 +++++----- 3 files changed, 69 insertions(+), 25 deletions(-) diff --git a/leetcode/701-800/0719.Find-K-th-Smallest-Pair-Distance/README.md b/leetcode/701-800/0719.Find-K-th-Smallest-Pair-Distance/README.md index 9b4c239fd..a1faa696b 100644 --- a/leetcode/701-800/0719.Find-K-th-Smallest-Pair-Distance/README.md +++ b/leetcode/701-800/0719.Find-K-th-Smallest-Pair-Distance/README.md @@ -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 +``` ## 结语 diff --git a/leetcode/701-800/0719.Find-K-th-Smallest-Pair-Distance/Solution.go b/leetcode/701-800/0719.Find-K-th-Smallest-Pair-Distance/Solution.go index d115ccf5e..e8697bc75 100644 --- a/leetcode/701-800/0719.Find-K-th-Smallest-Pair-Distance/Solution.go +++ b/leetcode/701-800/0719.Find-K-th-Smallest-Pair-Distance/Solution.go @@ -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 } diff --git a/leetcode/701-800/0719.Find-K-th-Smallest-Pair-Distance/Solution_test.go b/leetcode/701-800/0719.Find-K-th-Smallest-Pair-Distance/Solution_test.go index 14ff50eb4..cacf77682 100644 --- a/leetcode/701-800/0719.Find-K-th-Smallest-Pair-Distance/Solution_test.go +++ b/leetcode/701-800/0719.Find-K-th-Smallest-Pair-Distance/Solution_test.go @@ -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() { }