719. Find K-th Smallest Pair Distance
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;
}
};
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.
719. Find K-th Smallest Pair Distance的更多相关文章
- [LeetCode] 719. Find K-th Smallest Pair Distance 找第K小的数对儿距离
Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pai ...
- [LeetCode] Find K-th Smallest Pair Distance 找第K小的数对儿距离
Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pai ...
- [Swift]LeetCode719. 找出第 k 小的距离对 | Find K-th Smallest Pair Distance
Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pai ...
- 【leetcode】719. Find K-th Smallest Pair Distance
题目如下: 解题思路:对于这一类知道上限和下限,求第N位是什么的题目,可以先看看二分查找的方法可不可行.首先对nums进行排序,很显然任意两个元素距离绝对值最小是0,最大是nums[-1] - num ...
- Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.
In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentago ...
- 树形DP 统计树中长度为K的路径数量——Distance in Tree
一.问题描述 给出一棵n个节点的树,统计树中长度为k的路径的条数(1<=n<=50000 , 1<=k<=500). 二.解题思路 设d[i][k]表示以i为根节点长度为k的路 ...
- 【LeetCode】堆 heap(共31题)
链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...
- 【LeetCode】二分 binary_search(共58题)
[4]Median of Two Sorted Arrays [29]Divide Two Integers [33]Search in Rotated Sorted Array [34]Find F ...
- 排序矩阵中的从小到大第k个数 · Kth Smallest Number In Sorted Matrix
[抄题]: 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的定义为:每一行递增,每一列也递增. [思维问题]: 不知道应该怎么加,因为不是一维单调的. [一句话思路]: 周围两个数给x或y挪一 ...
随机推荐
- vmware workstation14永久激活密钥
vmware workstation14永久激活密钥分享: CG54H-D8D0H-H8DHY-C6X7X-N2KG6 ZC3WK-AFXEK-488JP-A7MQX-XL8YF AC5XK-0ZD4 ...
- AppStore审核--17.5
本文转载至 http://blog.csdn.net/addychen/article/details/39672185 感谢原文作者分享 AppStore审核 为了确保用户理解应用如何使用他们的数据 ...
- 使用 QWorker 做为计划任务引擎
QWorker 提供了 Plan 函数来提供计划任务功能的支持.每个任务做为一个作业,可以在指定的时间点被触发执行.而 cron 作为 Linux 操作系统下计划任务的标准被广大用户所熟知,QWork ...
- myeclipse10.0如何集成Git
现需要给myeclipse10.0集成git工具,经过搜索资料,现整理如下方法: myeclipse10.0对应的Git版本应该为:egit版本为2.3.1 下载地址:http://wiki.ecli ...
- 微信小程序开发:学习笔记[9]——本地数据缓存
微信小程序开发:学习笔记[9]——本地数据缓存 快速开始 说明 本地数据缓存是小程序存储在当前设备上硬盘上的数据,本地数据缓存有非常多的用途,我们可以利用本地数据缓存来存储用户在小程序上产生的操作,在 ...
- Loadrunner脚本自动关联和手动关联
关于Loadrunner关联一.什么时候需要关联 1.关联的含义 关联(correlation):在脚本回放过程中,客户端发出请求,通过关联函数所定义的左右边界值(也就是关联规则),在服 ...
- Java中锁的内存语义
我们都知道,Java中的锁可以让临界区互斥执行.锁是Java并发编程中最重要的同步机制,锁除了可以让临界区互斥执行外,还可以让释放锁的线程向获取同一个锁的线程发送消息.下面是锁的释放-获取的代码: C ...
- H264 各种profile
关键字:H264 ,base profile, main profile, extend profile, high profile. 提到High Profile H.264解码许多人并不了解,那么 ...
- 安装python解释器
Python目前已支持所有主流操作系统,在Linux,Unix,Mac系统上自带Python环境,在Windows系统上需要安装一下,超简单 打开官网 https://www.python.org/d ...
- Yii的缓存机制之动态缓存
当整个页面被缓存,但只有小部分区域需要根据不同的条件设置不同的信息.(例如商品的详细页面的缓存中用户名是动态的)这里就需要设置动态缓存. 首先在被缓存的模板中使用renderDynamic进行动态渲染 ...