719. Find K-th Smallest Pair Distance
2024-09-01 15:01:46
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:
2 <= len(nums) <= 10000
.0 <= nums[i] < 1000000
.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.
最新文章
- Event Handler
- Beta阶段第八次Scrum Meeting
- 洛谷 P2726 阶乘 Factorials Label:Water
- linux查看某个进程内存占用情况以及/proc/pid/status解释
- An invalid character [32] was present in the Cookie value
- 装饰者模式(Decorator pattern)
- 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
- Java log4j详细教程
- JDE FORM开发--checkBox
- MongoDB 3.2 在windows上的安装
- SAP中的Currency Converting Factor
- sftp配置
- 利用MyEclipes的反转工程来配置Hibernate各种配置
- MinGW-64 安装
- apache开源项目 -- Tuscany
- 思考 Swift 中的 MirrorType 协议
- RxJava RxAndroid【简介】
- Google 高性能 RPC 框架 gRPC 1.0.0 发布(附精彩评论)
- C# 开发系列(一)
- mysql进阶(二十八)MySQL GRANT REVOKE用法
热门文章
- SQL Cursor生命周期
- 文件共享和使用 dup 函数创建新描述符的区别
- iOS UI13_数据解析XML_,JSON
- 【BZOJ3837】[Pa2013]Filary 随机化神题
- This means that only a small number of nodes must be read from disk to retrieve an item.
- php date之间的相互转换
- LVS项目介绍
- 对xml文件的sax解析(增删改查)之一
- codeforces 466A. Cheap Travel 解题报告
- Tkinter图片按钮