当前位置:网站首页>[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;
}
边栏推荐
- Simple wechat applet development page Jump, data binding, obtaining user information, obtaining user location information
- ffmpeg录制屏幕和截屏
- [mathematical logic] predicate logic (judge whether the first-order predicate logic formula is true or false | explain | example | predicate logic formula type | forever true | forever false | satisfi
- 8.8.2-PointersOnC-20220214
- Shardingsphere dynamic data source
- Wechat applet + Alibaba IOT platform + Hezhou air724ug build a serverless IOT system (III) -- wechat applet is directly connected to Alibaba IOT platform aliiot
- Commands related to the startup of redis under Linux server (installation and configuration)
- Ffmpeg download and installation tutorial and introduction
- 2022 mobile crane driver examination registration and mobile crane driver operation examination question bank
- Mongodb master profile
猜你喜欢
Téléchargement et installation du client Filezilla
Is pytorch open source?
pytorch开源吗?
105. SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
PHP generates PDF tcpdf
Mongodb replication set [master-slave replication]
Hutool动态添加定时任务
Wechat applet + Alibaba IOT platform + Hezhou air724ug built with server version system analysis
Download and install node, NPM and yarn
IPv6过渡技术-6to4手工隧道配置实验--尚文网络奎哥
随机推荐
105. Detailed introduction of linkage effect realization of SAP ui5 master detail layout mode
2022 Shandong Province safety officer C certificate examination questions and Shandong Province safety officer C certificate simulation examination question bank
Interface embedded in golang struct
Applet get user avatar and nickname
Bisher - based on SSM pet adoption center
The latest analysis of the main principals of hazardous chemical business units in 2022 and the simulated examination questions of the main principals of hazardous chemical business units
[mathematical logic] propositional logic (propositional and connective review | propositional formula | connective priority | truth table satisfiable contradiction tautology)
Write it down once Net travel management background CPU Explosion Analysis
shardingsphere动态数据源
[Blue Bridge Road - bug free code] pcf8591 - code analysis of AD conversion
pytorch开源吗?
Nanning water leakage detection: warmly congratulate Guangxi Zhongshui on winning the first famous brand in Guangxi
2022 P cylinder filling examination content and P cylinder filling practice examination video
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
SAP UI5 应用开发教程之一百零五 - SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
The difference between static web pages and dynamic web pages & the difference between Web1.0 and Web2.0 & the difference between get and post
[Yu Yue education] reference materials of political communication science of Communication University of China
eth入门之简介
毕设-基于SSM宠物领养中心
没有sXid,suid&sgid将进入险境!-尚文网络xUP楠哥