当前位置:网站首页>[brush questions] find the number pair distance with the smallest K
[brush questions] find the number pair distance with the smallest K
2022-07-03 03:56:00 【m0_ sixty million six hundred and thirty-one thousand three hun】
One 、 subject
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 .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/find-k-th-smallest-pair-distance
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Two 、 Answer key
2.1 Ideas
2.2 Source code
Return value rightest The initial value of is set to -1, It's for a special situation , Is the given nums The data in the array is the same , So whatever is given K How much is the , The first K Small distances are 0, After entering the cycle rightest The value of has not changed or -1, Finally back to -1+1=0, accord with
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;
}
边栏推荐
- IPv6过渡技术-6to4手工隧道配置实验--尚文网络奎哥
- 2022deepbrainchain biweekly report no. 104 (01.16-02.15)
- C language hashtable/hashset library summary
- 递归使用和多维数组对象变一维数组对象
- Ffmpeg one / more pictures synthetic video
- [Yu Yue education] reference materials of political communication science of Communication University of China
- How to download pytorch? Where can I download pytorch?
- Some preliminary preparations for QQ applet development: make an appointment for a development account, download and install developer tools, and create QQ applet
- Wechat applet + Alibaba IOT platform + Hezhou air724ug built with server version system analysis
- [learning notes] seckill - seckill project - (11) project summary
猜你喜欢
SAP UI5 应用开发教程之一百零五 - SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
How to download pytorch? Where can I download pytorch?
FileZilla Client下载安装
Makefile demo
105. SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
2022 P cylinder filling examination content and P cylinder filling practice examination video
递归:一维链表和数组
Hutool dynamically adds scheduled tasks
简易版 微信小程序开发之for指令、上传图片及展示效果优化
Role of JS No
随机推荐
Without sxid, suid & sgid will be in danger- Shangwen network xUP Nange
[mathematical logic] predicate logic (first-order predicate logic formula | example)
2022 polymerization process examination questions and polymerization process examination skills
105. Detailed introduction of linkage effect realization of SAP ui5 master detail layout mode
Nodejs Foundation: shallow chat URL and querystring module
MPLS setup experiment
Makefile demo
MySQL MAC download and installation tutorial
pytorch是什么?pytorch是一个软件吗?
In Net 6 project using startup cs
NPM: the 'NPM' item cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Please check the spelling of the name. If the path is included, make sure the path is corr
Makefile demo
Half of 2022 is over, so we must hurry up
【全民编程】《软件编程-讲课视频》【零基础入门到实战应用】
Hutool动态添加定时任务
Shardingsphere dynamic data source
如何迈向IPv6之IPv6过渡技术-尚文网络奎哥
2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video
递归使用和多维数组对象变一维数组对象
C language hashtable/hashset library summary