当前位置:网站首页>【刷题篇】 找出第 K 小的数对距离
【刷题篇】 找出第 K 小的数对距离
2022-07-03 03:52:00 【m0_60631323】
一、题目
数对 (a,b) 由整数 a 和 b 组成,其数对距离定义为 a 和 b 的绝对差值。
给你一个整数数组 nums 和一个整数 k ,数对由 nums[i] 和 nums[j] 组成且满足 0 <= i < j < nums.length 。返回 所有数对距离中 第 k 小的数对距离。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-k-th-smallest-pair-distance
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
二、题解
2.1思路
2.2源码
返回值rightest的初始值设为-1,是为了一种特殊情况,就是给定的nums数组中的数据都是相同的,那么无论给定的K是多少,第K小的距离都是0,进入循环后rightest的值没有改变还是-1,最后返回-1+1=0,符合
public static int smallestDistancePair(int[] arr, int k) {
int n=arr.length;
if(n<2||k<1||k>(n*(n-1))>>1){
return -1;
}
Arrays.sort(arr);
int left=0;
int right=arr[n-1]-arr[0];
int mid=0;
int rightest=-1;
while (left<=right){
mid=(left+right)/2;
if(valid(arr,mid,k)){
rightest=mid;
left=mid+1;
}else {
right=mid-1;
}
}
return rightest+1;
}
public static boolean valid(int[] arr,int limit,int k){
int x=0;
for (int l=0,r=1;l<arr.length;r=Math.max(r,++l)){
while (r< arr.length&&(arr[r]-arr[l]<=limit)){
r++;
}
x+=r-l-1;
}
return x<k;
}
边栏推荐
- [Apple Photo Album push] IMessage group anchor local push
- 2020-01-01t00:00:00.000000z date format conversion
- node,npm以及yarn下载安装
- pytorch怎么下载?pytorch在哪里下载?
- Recursion: depth first search
- [mathematical logic] propositional logic (equivalent calculus | idempotent law | exchange law | combination law | distribution law | De Morgan law | absorption rate | zero law | identity | exclusion l
- [DRM] simple analysis of DRM bridge driver call process
- PHP generates PDF tcpdf
- Small guide for rapid formation of manipulator (VIII): kinematic modeling (standard DH method)
- 2022deepbrainchain biweekly report no. 104 (01.16-02.15)
猜你喜欢
递归:一维链表和数组
Error in compiled file: error: unmapped character encoding GBK
Latest version of NPM: the "NPM" item cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Please check
Ansible introduction [unfinished (semi-finished products)]
numpy之 警告VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences
Wechat applet + Alibaba IOT platform + Hezhou air724ug built with server version system analysis
编译文件时报错:错误: 编码GBK的不可映射字符
CEPH Shangwen network xUP Nange that releases the power of data
FileZilla Client下载安装
2022 mobile crane driver examination registration and mobile crane driver operation examination question bank
随机推荐
[daily question] dichotomy - find a single dog (Bushi)
2022 tea master (primary) examination questions and tea master (primary) examination question bank
静态网页 和 动态网页的区别 & WEB1.0和WEB2.0的区别 & GET 和 POST 的区别
Role of JS No
Simple wechat applet development page Jump, data binding, obtaining user information, obtaining user location information
第十届中国云计算大会·中国站:展望未来十年科技走向
Is pytorch open source?
Applet (continuous update)
Use three JS make a simple 3D scene
Applet get user avatar and nickname
2.14 simulation summary
pytorch难学吗?如何学好pytorch?
[MySQL] the difference between left join, right join and join
Makefile demo
MongoDB基本操作【增、删、改、查】
Hutool动态添加定时任务
[national programming] [software programming - Lecture Video] [zero foundation introduction to practical application]
2020-01-01t00:00:00.000000z date format conversion
The difference between static web pages and dynamic web pages & the difference between Web1.0 and Web2.0 & the difference between get and post
MongoDB简介