当前位置:网站首页>【刷题篇】 找出第 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;
}
边栏推荐
- [mathematical logic] propositional logic (equivalent calculus | idempotent law | exchange law | combination law | distribution law | De Morgan law | absorption rate | zero law | identity | exclusion l
- Is pytorch difficult to learn? How to learn pytorch well?
- For instruction, uploading pictures and display effect optimization of simple wechat applet development
- 简易版 微信小程序开发之页面跳转、数据绑定、获取用户信息、获取用户位置信息
- Wechat applet + Alibaba IOT platform + Hezhou air724ug built with server version system analysis
- C语言HashTable/HashSet库汇总
- Téléchargement et installation du client Filezilla
- Using jasmine to monitor constructors - spying on a constructor using Jasmine
- Web session management security issues
- Latest version of NPM: the "NPM" item cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Please check
猜你喜欢

Makefile demo

Is pytorch open source?

Pytorch multi card distributed training distributeddataparallel usage

CEPH Shangwen network xUP Nange that releases the power of data

Ffmpeg download and installation tutorial and introduction

What can learning pytorch do?

MongoDB簡介

Is pytorch difficult to learn? How to learn pytorch well?

Recursion: quick sort, merge sort and heap sort

Recursion: depth first search
随机推荐
Error in compiled file: error: unmapped character encoding GBK
Applet get user avatar and nickname
Docker install and start MySQL service
FileZilla client download and installation
Error c2694 "void logger:: log (nvinfer1:: ilogger:: severity, const char *)": rewrite the restrictive exception specification of virtual functions than base class virtual member functions
释放数据力量的Ceph-尚文网络xUP楠哥
Hutool动态添加定时任务
sigaction的使用
动态规划:最长公共子串和最长公共子序列
Arlo's thinking about himself
递归使用和多维数组对象变一维数组对象
Download and install captura and configure ffmpeg in captura
Leetcode: dynamic planning template
Simple wechat applet development page Jump, data binding, obtaining user information, obtaining user location information
ffmpeg下载安装教程及介绍
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
Message queue addition failure
C language hashtable/hashset library summary
How to execute a swift for in loop in one step- How can I do a Swift for-in loop with a step?
[mathematical logic] propositional logic (propositional logic reasoning | formal structure of reasoning | inference law | additional law | simplification law | hypothetical reasoning | refusal | disju