当前位置:网站首页>【刷题篇】 找出第 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;
}
边栏推荐
- Wechat applet + Alibaba IOT platform + Hezhou air724ug build a serverless IOT system (III) -- wechat applet is directly connected to Alibaba IOT platform aliiot
- [national programming] [software programming - Lecture Video] [zero foundation introduction to practical application]
- Intercept string fixed length to array
- 递归使用和多维数组对象变一维数组对象
- Debug: CD cannot be used in kaggle
- What can learning pytorch do?
- 【全民编程】《软件编程-讲课视频》【零基础入门到实战应用】
- [set theory] partial order relation (partial order relation definition | partial order set definition | greater than or equal to relation | less than or equal to relation | integer division relation |
- Half of 2022 is over, so we must hurry up
- Error in compiled file: error: unmapped character encoding GBK
猜你喜欢

SAP ui5 application development tutorial 105 - detailed introduction to the linkage effect implementation of SAP ui5 master detail layout mode

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

Small guide for rapid formation of manipulator (VIII): kinematic modeling (standard DH method)

递归:快速排序,归并排序和堆排序

How to move towards IPv6: IPv6 Transition Technology - Shangwen network quigo

In Net 6 project using startup cs

node,npm以及yarn下载安装

Wechat applet + Alibaba IOT platform + Hezhou air724ug build a serverless IOT system (III) -- wechat applet is directly connected to Alibaba IOT platform aliiot
![[Apple Photo Album push] IMessage group anchor local push](/img/a7/6a27d646ecba0d7c93f8dac38492a2.jpg)
[Apple Photo Album push] IMessage group anchor local push

Introduction à mongodb
随机推荐
Separable bonds and convertible bonds
What can learning pytorch do?
Numpy warning visibledeprecationwarning: creating an ndarray from ragged needed sequences
Ffmpeg one / more pictures synthetic video
Ansible introduction [unfinished (semi-finished products)]
Recursion: quick sort, merge sort and heap sort
Null and undefined
[mathematical logic] propositional logic (propositional and connective review | propositional formula | connective priority | truth table satisfiable contradiction tautology)
numpy之 警告VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences
[mathematical logic] propositions and connectives (propositions | propositional symbolization | truth connectives | no | conjunction | disjunction | non truth connectives | implication | equivalence)
Mongodb installation & Deployment
sigaction的使用
Pytorch multi card distributed training distributeddataparallel usage
【学习笔记】seckill-秒杀项目--(11)项目总结
leetcode:动态规划模板
[embedded module] OLED display module
Hutool动态添加定时任务
错误 C2694 “void Logger::log(nvinfer1::ILogger::Severity,const char *)”: 重写虚函数的限制性异常规范比基类虚成员函数
Nanning water leakage detection: warmly congratulate Guangxi Zhongshui on winning the first famous brand in Guangxi
记一次 .NET 差旅管理后台 CPU 爆高分析