当前位置:网站首页>719. find the distance of the number pair with the smallest K (two points)
719. find the distance of the number pair with the smallest K (two points)
2022-06-29 07:44:00 【Wubeibei 97】
Number pair (a,b) By integer a and b form , The number pair distance is defined as a and b The absolute difference between .
Give you an array of integers nums And an integer k , Number pair by nums[i] and nums[j] Constitute and satisfy 0 <= i < j < nums.length . return All pairs of distances The first k Small number pair distance .
Example 1:
Input :nums = [1,3,1], k = 1
Output :0
explain : The number pairs and the corresponding distances are as follows :
(1,3) -> 2
(1,1) -> 0
(3,1) -> 2
Distance No 1 A small number pair is (1,1) , A distance of 0 .
Example 2:
Input :nums = [1,1,1], k = 2
Output :0
Example 3:
Input :nums = [1,6,1], k = 3
Output :5
class Solution {
public:
int smallestDistancePair(vector<int>& nums, int k) {
// Sort service enumeration interval
sort(nums.begin(), nums.end());
int left = 0, right = nums[nums.size() - 1], n = nums.size();
while(left < right){
int mid = (left + right) >> 1, cnt = 0;
// Enumeration interval
for(int i = 0, j = 0; i < n; ++i){
// Find the first one that doesn't meet the condition j
while(j < n && nums[j] - nums[i] <= mid) ++j;
cnt += j - i - 1;
}
// Determine bisection direction
if(cnt < k)
left = mid + 1;
else
right = mid;
}
return left;
}
};
边栏推荐
- Viewing application and installation of Hana database license
- 蓝桥杯——最小框架
- Vibration signal generation and processing based on MATLAB Doppler effect
- 【FreeRTOS】中断机制
- Mmclassification installation and debugging
- TREE ALV 展开Node或者点击Toolbar按钮时DUMP(CL_ALV_TREE_BASE==============CP|SET_ITEMS_FOR_COLUMN)
- tf. to_ int64
- 关于开发web场景下如何解决手机访问web跨域问题
- Appium自动化测试基础 — ADB常用命令(三)
- excel高级绘图技巧100讲(六)-甘特图在项目进度上的实战应用案例
猜你喜欢

阿里云访问资源:NoSuchKey

Interviewer: why does database connection consume resources? Where are the resources consumed?

Postman pre request

Appium automation test foundation ADB common commands (II)

Using cdockablepane to realize floating window in MFC

How to talk about salary correctly in software test interview?

施努卡:3d机器视觉检测系统 3d视觉检测应用行业

1183: patient queue

Mmclassification installation and debugging

Detailed explanation of top and free commands
随机推荐
Two ways to write throttling - recently seen
Use of parameter in Simulink for AUTOSAR SWC
Markdown skill tree (4): link
Deploy Prometheus server service system management
matlab simulink 电网扫频仿真和分析
Simulation analysis of sailing dynamics
Appium environment setup
KingbbaseES V8R6集群维护案例之---集群之间数据迁移
Problem solving -- > online OJ (13)
ES中配置ext.dic文件不生效的原因
【工控老马】基于西门子S7-200PLC的跑马灯控制系统的设计方案详解
Cross domain data request using jsonp
Use of LSTM neural network and general neural network
关于KingbaseES临时文件过大问题
Appium自动化测试基础 — ADB常用命令(二)
flutter 仿照 uiswitch
Schnuka: 3D visual recognition system 3D visual inspection principle
tf. compat. v1.assign
Check whether tensorflow supports GPU and test program
九州云助力内蒙古“东数西算”工程,驱动测绘行业智慧新生态