当前位置:网站首页>【刷题篇】 找出第 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;
}
边栏推荐
- pytorch开源吗?
- Wechat applet + Alibaba IOT platform + Hezhou air724ug build a serverless IOT system (III) -- wechat applet is directly connected to Alibaba IOT platform aliiot
- docker安装及启动mysql服务
- [combinatorics] brief introduction to generating function (definition of generating function | Newton binomial coefficient | commonly used generating function | correlation with constant | correlation
- Is pytorch open source?
- C language hashtable/hashset library summary
- node,npm以及yarn下载安装
- Table structure of Navicat export database
- 阿洛对自己的思考
- PHP generates PDF tcpdf
猜你喜欢

Download and install node, NPM and yarn

Mongodb installation & Deployment

2022 tea master (primary) examination questions and tea master (primary) examination question bank

如何迈向IPv6之IPv6过渡技术-尚文网络奎哥

Makefile demo

Role of JS No

pytorch怎么下载?pytorch在哪里下载?

Introduction à mongodb

Pytorch multi card distributed training distributeddataparallel usage
![Mongodb replication set [master-slave replication]](/img/2c/8030548455f45fa252062dd90e7b8b.png)
Mongodb replication set [master-slave replication]
随机推荐
Null and undefined
如何迈向IPv6之IPv6过渡技术-尚文网络奎哥
Avec trois. JS fait une scène 3D simple
Introduction to mongodb
2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video
Tidal characteristics of the Bohai Sea and the Yellow Sea
2022年已过半,得抓紧
SAP ui5 application development tutorial 105 - detailed introduction to the linkage effect implementation of SAP ui5 master detail layout mode
Intercept string fixed length to array
在写web项目的时候,文件上传用到了smartupload,用了new string()进行转码,但是在数据库中,还是会出现类似扑克的乱码
Mysql Mac版下载安装教程
Wechat applet + Alibaba IOT platform + Hezhou air724ug build a serverless IOT system (III) -- wechat applet is directly connected to Alibaba IOT platform aliiot
2022-07-02:以下go语言代码输出什么?A:编译错误;B:Panic;C:NaN。 package main import “fmt“ func main() { var a =
Filter
Role of JS No
【DRM】DRM bridge驱动调用流程简单分析
动态规划:最长回文子串和子序列
Numpy warning visibledeprecationwarning: creating an ndarray from ragged needed sequences
QQ小程序开发之 一些前期准备:预约开发账号、下载安装开发者工具、创建qq小程序
[national programming] [software programming - Lecture Video] [zero foundation introduction to practical application]