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挪一 ...
随机推荐
- Android组件系列----ContentProvider内容提供者【4】
(4)单元測试类: 这里须要涉及到另外一个知识:ContentResolver内容訪问者. 要想訪问ContentProvider.则必须使用ContentResolver. 能够通过ContentR ...
- MongoDB之增删改查(一)
本文主要介绍MongoDB数据库增删改查操作. 增 mongoDB和其它关系型数据库一样,通过insert来添加数据到集合中去. db.collectionName.insert(内容) 显示数据库中 ...
- 有趣的Ruby-学习笔记3
Ruby方法 方法名要以小写字母开头.假设用大写字母开头会被作为常量 (这点非常奇怪) 定义一个无參的方法 def method_name expr.. end 定义一个有參的方法 def metho ...
- OpenStack部署到Hadoop的四种方案
随着企业開始同一时候利用云计算和大数据技术.如今应当考虑怎样将这些工具结合使用.在这样的情况下,企业将实现最佳的分析处理能力.同一时候利用私有云的高速弹性 (rapid elasticity) 和单一 ...
- 九度OJ 1118:数制转换 (进制转换)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3873 解决:1494 题目描述: 求任意两个不同进制非负整数的转换(2进制-16进制),所给整数在long所能表达的范围之内. 不 ...
- mybatis入门小结(六)
入门小结---查询 1.1.1.1.1 #{}和${} #{}表示一个占位符号,通过#{}可以实现preparedStatement向占位符中设置值,自动进行java类型和jdbc类型转换,#{}可以 ...
- SpringBoot-(5)-properties的使用
项目中经常需要进行一些配置,一般会使用springboot默认的application.properties文件,也可以自己创建配置文件 一,application.properties配置 logg ...
- SYN FLOOD学习理解
SYN FLOOD是一种比较常见的DoS攻击手段,它的特点就是防不胜防.SYN攻击属于DOS攻击的一种,它利用TCP协议缺陷,通过发送大量的半连接请求,耗费CPU和内存资源.SYN攻击除了能影响主机外 ...
- codeforces 的 Codeforces Round #273 (Div. 2) --C Table Decorations
C. Table Decorations time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Java 发送Get和Post请求
package com.htpt.superviseServices.dm.util; import java.io.BufferedReader; import java.io.IOExceptio ...