[Swift]LeetCode719. 找出第 k 小的距離對 | Find K-th Smallest Pair Distance

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-epulugft-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B.git

Example 1:github

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. 

Note:數組

  1. 2 <= len(nums) <= 10000.
  2. 0 <= nums[i] < 1000000.
  3. 1 <= k <= len(nums) * (len(nums) - 1) / 2.

給定一個整數數組,返回全部數對之間的第 k 個最小距離。一對 (A, B) 的距離被定義爲 A 和 B 之間的絕對差值。微信

示例 1:spa

輸入:
nums = [1,3,1]
k = 1
輸出:0 
解釋:
全部數對以下:
(1,3) -> 2
(1,1) -> 0
(3,1) -> 2
所以第 1 個最小距離的數對是 (1,1),它們之間的距離爲 0。

提示:code

  1. 2 <= len(nums) <= 10000.
  2. 0 <= nums[i] < 1000000.
  3. 1 <= k <= len(nums) * (len(nums) - 1) / 2.

Runtime: 68 ms
Memory Usage: 19.6 MB
 1 class Solution {
 2     func smallestDistancePair(_ nums: [Int], _ k: Int) -> Int {
 3         var nums = nums.sorted(by:<)
 4         var n:Int = nums.count
 5         var left:Int = 0
 6         var right:Int = nums.last! - nums[0]
 7         while (left < right) 
 8         {
 9             var mid:Int = left + (right - left) / 2
10             var cnt:Int = 0
11             var start:Int = 0
12             for i in 0..<n
13             {
14                 while (start < n && nums[i] - nums[start] > mid)
15                 {
16                     start += 1
17                 }
18                 cnt += i - start
19             }
20             if cnt < k
21             {
22                 left = mid + 1
23             }
24             else
25             {
26                 right = mid
27             }
28         }
29         return right       
30     }
31 }

72mshtm

 1 class Solution {
 2   func smallestDistancePair(_ nums: [Int], _ k: Int) -> Int {
 3     var sorted = nums
 4     sorted.sort { $0 < $1 }
 5     
 6     var lo = 0
 7     var hi = sorted[sorted.count - 1] - sorted[0]
 8     while lo < hi {
 9       let m = lo + ((hi - lo) / 2)
10       var count = 0
11       var j = 0
12       for i in 0 ..< sorted.count - 1 {
13         while j < sorted.count && sorted[j] - sorted[i] <= m {
14           j += 1
15         }
16         count += j - i - 1
17       }
18       if count < k {
19         lo = m + 1
20       } else {
21         hi = m
22       }
23     }
24     return lo
25   }
26 }

88msblog

 1 class Solution {
 2     func smallestDistancePair(_ nums: [Int], _ k: Int) -> Int {
 3         let nums = nums.sorted()
 4         var low = 0, high = abs((nums.first ?? 0) - (nums.last ?? 0))
 5         while low < high {
 6             let m = (low + high) / 2
 7             var count = 0
 8             var j = 1
 9             for i in 0..<nums.count-1 {
10                 while j < nums.count && nums[j] - nums[i] <= m { j += 1 }
11                 count += j-i-1;
12             }
13             if count < k {
14                 low = m + 1
15             } else {
16                 high = m
17             }
18         }
19         return low
20     }
21 }
相關文章
相關標籤/搜索