当前位置:网站首页>【刷题篇】 找出第 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;
}
边栏推荐
- Commands related to the startup of redis under Linux server (installation and configuration)
- Summary of electromagnetic spectrum
- npm : 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
- Recursive use and multi-dimensional array object to one-dimensional array object
- Avec trois. JS fait une scène 3D simple
- Without sxid, suid & sgid will be in danger- Shangwen network xUP Nange
- 可分离债券与可转债
- navicat 导出数据库的表结构
- 2022deepbrainchain biweekly report no. 104 (01.16-02.15)
- Shardingsphere dynamic data source
猜你喜欢
2022 polymerization process examination questions and polymerization process examination skills
简易版 微信小程序开发之for指令、上传图片及展示效果优化
Wechat applet + Alibaba IOT platform + Hezhou air724ug build a serverless IOT system (III) -- wechat applet is directly connected to Alibaba IOT platform aliiot
pytorch难学吗?如何学好pytorch?
QQ小程序开发之 一些前期准备:预约开发账号、下载安装开发者工具、创建qq小程序
ffmpeg下载安装教程及介绍
小程序获取用户头像和昵称
How to download pytorch? Where can I download pytorch?
Small guide for rapid formation of manipulator (VIII): kinematic modeling (standard DH method)
MongoDB簡介
随机推荐
Simple wechat applet development page Jump, data binding, obtaining user information, obtaining user location information
2022 P cylinder filling examination content and P cylinder filling practice examination video
2.14 simulation summary
[MySQL] the difference between left join, right join and join
How to execute a swift for in loop in one step- How can I do a Swift for-in loop with a step?
Use of sigaction
SAP UI5 应用开发教程之一百零五 - SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
用Three.js做一個簡單的3D場景
Applet (continuous update)
ffmpeg录制屏幕和截屏
2022 mobile crane driver examination registration and mobile crane driver operation examination question bank
Ffmpeg download and installation tutorial and introduction
C language hashtable/hashset library summary
Separable bonds and convertible bonds
Mongodb replication set [master-slave replication]
IPv6过渡技术-6to4手工隧道配置实验--尚文网络奎哥
[mathematical logic] propositional logic (propositional logic reasoning | formal structure of reasoning | inference law | additional law | simplification law | hypothetical reasoning | refusal | disju
docker安装及启动mysql服务
Error in compiled file: error: unmapped character encoding GBK
错误 C2694 “void Logger::log(nvinfer1::ILogger::Severity,const char *)”: 重写虚函数的限制性异常规范比基类虚成员函数