当前位置:网站首页>719. find the distance of the number pair with the smallest K (two points)
719. find the distance of the number pair with the smallest K (two points)
2022-06-29 07:44:00 【Wubeibei 97】
Number pair (a,b) By integer a and b form , The number pair distance is defined as a and b The absolute difference between .
Give you an array of integers nums And an integer k , Number pair by nums[i] and nums[j] Constitute and satisfy 0 <= i < j < nums.length . return All pairs of distances The first k Small number pair distance .
Example 1:
Input :nums = [1,3,1], k = 1
Output :0
explain : The number pairs and the corresponding distances are as follows :
(1,3) -> 2
(1,1) -> 0
(3,1) -> 2
Distance No 1 A small number pair is (1,1) , A distance of 0 .
Example 2:
Input :nums = [1,1,1], k = 2
Output :0
Example 3:
Input :nums = [1,6,1], k = 3
Output :5
class Solution {
public:
int smallestDistancePair(vector<int>& nums, int k) {
// Sort service enumeration interval
sort(nums.begin(), nums.end());
int left = 0, right = nums[nums.size() - 1], n = nums.size();
while(left < right){
int mid = (left + right) >> 1, cnt = 0;
// Enumeration interval
for(int i = 0, j = 0; i < n; ++i){
// Find the first one that doesn't meet the condition j
while(j < n && nums[j] - nums[i] <= mid) ++j;
cnt += j - i - 1;
}
// Determine bisection direction
if(cnt < k)
left = mid + 1;
else
right = mid;
}
return left;
}
};
边栏推荐
- Viewing application and installation of Hana database license
- Es query syntax
- Kingbasees coping with transaction rollback caused by too fast growth of table age
- Schnuka: visual positioning system manufacturer what is a visual positioning system
- Deploy Prometheus server service system management
- Detailed explanation of shell condition judgment
- 施努卡:轮胎自动抓取安装,3D视觉定位,机器人自动抓取
- SQL 注入绕过(六)
- tf.count_nonzero
- Suggestions on working methods and efficient work
猜你喜欢

编译原理王者之路

感知健康生活 赋能无界连接 ——为OpenHarmony 3.1生态构建贡献芯海力量

Matlab Simulink simulation and analysis of power grid sweep frequency

Alternative writing of if else in a project

【FreeRTOS】中断机制

Viewing application and installation of Hana database license

Some examples.
How to view software testing training? Do you need training?

cv2.cvtColor

Appium自动化测试基础 — ADB常用命令(三)
随机推荐
4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?
编译原理王者之路
101. 对称二叉树(递归与迭代方法)
关于工作方法和高效工作的建议
Markdown skill tree (6): List
Cross domain data request using jsonp
Compiling principle: the king's way
关于开发web场景下如何解决手机访问web跨域问题
如何看待软件测试培训?你需要培训吗?
Using cdockablepane to realize floating window in MFC
KingbaseES 中select distinct on 语句
tf. count_ nonzero
[FreeRTOS] interrupt mechanism
蓝桥杯——最小框架
matlab simulink 电网扫频仿真和分析
【工控老马】单片机与西门子S7-200通信原理详解
How to view software testing training? Do you need training?
498. 对角线遍历(模拟)
1032 Sharing
tf. compat. v1.assign