2 Star 10 Fork 2

CG国斌 / myleetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
_719.java 1.28 KB
一键复制 编辑 原始数据 按行查看 历史
Charies Gavin 提交于 2020-02-06 12:44 . 初始化 myleetcode 项目
package com.hit.basmath.learn.binary_search;
import java.util.Arrays;
/**
* 719. Find K-th Smallest Pair Distance
* <p>
* Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B.
* <p>
* Example 1:
* <p>
* Input:
* nums = [1,3,1]
* k = 1
* Output: 0
* <p>
* Explanation:
* <p>
* Here are all the pairs:
* <p>
* (1,3) -> 2
* (1,1) -> 0
* (3,1) -> 2
* <p>
* Then the 1st smallest distance pair is (1,1), and its distance is 0.
* <p>
* Note:
* <p>
* 2 <= len(nums) <= 10000.
* 0 <= nums[i] < 1000000.
* 1 <= k <= len(nums) * (len(nums) - 1) / 2.
*/
public class _719 {
public int smallestDistancePair(int[] nums, int k) {
Arrays.sort(nums);
int n = nums.length;
int left = 0;
int right = nums[n - 1] - nums[0];
for (int cnt = 0; left < right; cnt = 0) {
int mid = left + (right - left) / 2;
for (int i = 0, j = 0; i < n; i++) {
while (j < n && nums[j] <= nums[i] + mid) j++;
cnt += j - i - 1;
}
if (cnt < k) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
}
Java
1
https://gitee.com/guobinhit/myleetcode.git
git@gitee.com:guobinhit/myleetcode.git
guobinhit
myleetcode
myleetcode
master

搜索帮助