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.

Example 1:

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.

Approach #1: Brute Force.[Memory Limit Exceeded]

class Solution {
public:
int smallestDistancePair(vector<int>& nums, int k) {
int len = nums.size();
vector<int> temp;
for (int i = 0; i < len; ++i) {
for (int j = i+1; j < len; ++j) {
int sub = abs(nums[i] - nums[j]);
temp.push_back(sub);
}
}
sort(temp.begin(), temp.end());
return temp[k-1];
}
};

  

Approach #2: Brute Force. [Bucter Sort]

class Solution {
public:
int smallestDistancePair(vector<int>& nums, int k) {
int len = nums.size();
sort(nums.begin(), nums.end());
int N = nums.back();
vector<int> container(N+1, 0);
for (int i = 0; i < len; ++i) {
for (int j = i+1; j < len; ++j) {
++container[nums[j]-nums[i]];
}
}
for (int i = 0; i <= N; ++i) {
k -= container[i];
if (k <= 0) return i;
}
return 0;
}
};

Runtime: 580 ms, faster than 13.29% of C++ online submissions for Find K-th Smallest Pair Distance.

Approach #2: Binary Search + DP

class Solution {
public:
int smallestDistancePair(vector<int>& nums, int k) {
int len = nums.size();
sort(nums.begin(), nums.end());
int l = 0;
int r = nums.back() - nums.front();
while (l <= r) {
int count = 0;
int j = 0;
int m = l + (r - l) / 2;
for (int i = 0; i < len; ++i) {
while (j < len && nums[j] - nums[i] <= m) j++;
count += j - i - 1;
}
count >= k ? r = m - 1 : l = m + 1;
}
return l;
}
};
Runtime: 16 ms, faster than 43.93% of C++ online submissions for Find K-th Smallest Pair Distance.

 Analysis:
nums = [1, 1, 3, 5, 8] total pairs = 10
suppose: m = 3
i : nums[i] j : nums[j] dp[i] pairs
0 : 1 3 : 5 2 1:1; 1:3
1 : 1 3 : 5 3 1:3
2 : 3 4 : 8 4 3:5
3 : 5 end 5 5:8

dp[i] = dp[i-1] + (j - i - 1);

we can use a constant to instead dp array.

最新文章

  1. Event Handler
  2. Beta阶段第八次Scrum Meeting
  3. 洛谷 P2726 阶乘 Factorials Label:Water
  4. linux查看某个进程内存占用情况以及/proc/pid/status解释
  5. An invalid character [32] was present in the Cookie value
  6. 装饰者模式(Decorator pattern)
  7. spark mllib配置pom.xml错误 Multiple markers at this line Could not transfer artifact net.sf.opencsv:opencsv:jar:2.3 from/to central (https://repo.maven.apache.org/maven2): repo.maven.apache.org
  8. Java log4j详细教程
  9. JDE FORM开发--checkBox
  10. MongoDB 3.2 在windows上的安装
  11. SAP中的Currency Converting Factor
  12. sftp配置
  13. 利用MyEclipes的反转工程来配置Hibernate各种配置
  14. MinGW-64 安装
  15. apache开源项目 -- Tuscany
  16. 思考 Swift 中的 MirrorType 协议
  17. RxJava RxAndroid【简介】
  18. Google 高性能 RPC 框架 gRPC 1.0.0 发布(附精彩评论)
  19. C# 开发系列(一)
  20. mysql进阶(二十八)MySQL GRANT REVOKE用法

热门文章

  1. SQL Cursor生命周期
  2. 文件共享和使用 dup 函数创建新描述符的区别
  3. iOS UI13_数据解析XML_,JSON
  4. 【BZOJ3837】[Pa2013]Filary 随机化神题
  5. This means that only a small number of nodes must be read from disk to retrieve an item.
  6. php date之间的相互转换
  7. LVS项目介绍
  8. 对xml文件的sax解析(增删改查)之一
  9. codeforces 466A. Cheap Travel 解题报告
  10. Tkinter图片按钮