当前位置:网站首页>【刷题篇】 找出第 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;
}
边栏推荐
- Download and install node, NPM and yarn
- 没有sXid,suid&sgid将进入险境!-尚文网络xUP楠哥
- Avec trois. JS fait une scène 3D simple
- [Blue Bridge Road -- bug free code] DS18B20 temperature reading code analysis
- Use of sigaction
- navicat 导出数据库的表结构
- Recursion: one dimensional linked lists and arrays
- Message queue addition failure
- Without sxid, suid & sgid will be in danger- Shangwen network xUP Nange
- Nanning water leakage detection: warmly congratulate Guangxi Zhongshui on winning the first famous brand in Guangxi
猜你喜欢

CEPH Shangwen network xUP Nange that releases the power of data

pytorch项目怎么跑?

Without sxid, suid & sgid will be in danger- Shangwen network xUP Nange

Recursion: depth first search

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

QQ小程序开发之 一些前期准备:预约开发账号、下载安装开发者工具、创建qq小程序

Some preliminary preparations for QQ applet development: make an appointment for a development account, download and install developer tools, and create QQ applet
![Ansible introduction [unfinished (semi-finished products)]](/img/2a/0003daf761ba02d8837c4657fc3f29.png)
Ansible introduction [unfinished (semi-finished products)]

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

第十届中国云计算大会·中国站:展望未来十年科技走向
随机推荐
How to move towards IPv6: IPv6 Transition Technology - Shangwen network quigo
Web session management security issues
小程序获取用户头像和昵称
动态规划:最长回文子串和子序列
pytorch是什么?pytorch是一个软件吗?
SAP UI5 应用开发教程之一百零五 - SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
[Blue Bridge Road -- bug free code] interpretation of some codes of matrix keyboard
MongoDB基本操作【增、删、改、查】
Role of JS No
Introduction à mongodb
2022deepbrainchain biweekly report no. 104 (01.16-02.15)
Web会话管理安全问题
Message queue addition failure
[learning notes] seckill - seckill project - (11) project summary
Leetcode: dynamic planning template
递归使用和多维数组对象变一维数组对象
Mongodb installation & Deployment
Download and install captura and configure ffmpeg in captura
docker安装及启动mysql服务
Reflection and planning of a sophomore majoring in electronic information engineering