2019.11.13 從零單刷個人筆記整理(持續更新)](https://blog.csdn.net/qq_20304723/article/details/89401203)
github:https://github.com/ChopinXBP/LeetCode-Babel
這題可以有一個直接的思路是用最大堆+歸併,對於排序後的數組中的每一個元素nums[i],左側最小的距離必定爲nums[i]-nums[i-1],其後到nums[i]-nums[i-2]……,因此只要進行nums.length路歸併,將k個距離加入最大堆,即可求得。
但是這種方法在糟糕情況下可能會超時。標準解法是二分查找,比較難想。
首先要意識到:第k小的距離一定出現在[0, max-min]之中,對此距離進行二分查找。
方法1:二分查找+前綴和
計算sameValue[i]代表在i之前且和i相等的元素個數,計算smallerNums[i]表示≤當前座標i的元素個數。第k小的距離一定出現在[0, max-min]之中,對此距離進行二分查找,
對於每一個位置i,滿足i < j && nums[j]-nums[i] ≤ mid的j的個數爲:從nums[i]到nums[i]+mid的元素個數(前綴和) + 在i之前且和i相等的元素個數。因此:
count = smallerNums[nums[i] + mid] - smallerNums[nums[i]] + sameValue[i];
對所有i進行遍歷加和,即爲≤mid的距離對個數。
方法2:二分查找+雙指針
第k小的距離一定出現在[0, max-min]之中,對此距離進行二分查找。對於每一個位置left,滿足left< right && nums[right]-nums[left]≤mid的j的個數可以通過雙指針遍歷求得。
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.
給定一個整數數組,返回所有數對之間的第 k 個最小距離。一對 (A, B) 的距離被定義爲 A 和 B 之間的絕對差值。
示例 1:
輸入:
nums = [1,3,1]
k = 1
輸出:0
解釋:
所有數對如下:
(1,3) -> 2
(1,1) -> 0
(3,1) -> 2
因此第 1 個最小距離的數對是 (1,1),它們之間的距離爲 0。
提示:
2 <= len(nums) <= 10000.
0 <= nums[i] < 1000000.
1 <= k <= len(nums) * (len(nums) - 1) / 2.
import java.util.Arrays;
/**
*
* 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.
* 給定一個整數數組,返回所有數對之間的第 k 個最小距離。一對 (A, B) 的距離被定義爲 A 和 B 之間的絕對差值。
*
*/
public class FindKthSmallestPairDistance {
//二分查找+前綴和
public int smallestDistancePair(int[] nums, int k) {
Arrays.sort(nums);
//二分查找中前綴和計算所需最大距離爲最大值的兩倍
int width = nums[nums.length - 1] << 1;
//sameValue[i]代表在i之前且和i相等的元素個數
int[] sameValue = new int[nums.length];
for(int i = 1; i < nums.length; i++){
if(nums[i] == nums[i - 1]){
sameValue[i] = sameValue[i - 1] + 1;
}
}
//smallerNums[i]表示≤當前座標i的元素個數
int[] smallerNums = new int[width];
int left = 0;
for(int i = 0; i < width; i++){
while(left < nums.length && nums[left] <= i){
left++;
}
smallerNums[i] = left;
}
//第k小的距離一定出現在[0, max-min]之中,對此距離進行二分查找
int begin = 0;
int end = nums[nums.length - 1] - nums[0];
while(begin < end){
int mid = (begin + end) >> 1;
//對於每一個位置i,滿足i<j && nums[j]-nums[i]≤mid的j的個數爲:從nums[i]到nums[i]+mid的元素個數(前綴和) + 在i之前且和i相等的元素個數
//smallerNums[nums[i] + mid] - smallerNums[nums[i]] + sameValue[i]
int count = 0;
for(int i = 0; i < nums.length; i++){
count += smallerNums[nums[i] + mid] - smallerNums[nums[i]] + sameValue[i];
}
if(count >= k){
end = mid;
}else{
begin = mid + 1;
}
}
return begin;
}
//二分查找+雙指針
public int smallestDistancePair2(int[] nums, int k) {
Arrays.sort(nums);
//第k小的距離一定出現在[0, max-min]之中,對此距離進行二分查找
int begin = 0;
int end = nums[nums.length - 1] - nums[0];
while(begin < end){
int mid = (begin + end) >> 1;
//對於每一個位置left,滿足left<right && nums[right]-nums[left]≤mid的j的個數可以通過雙指針遍歷求得
int count = 0;
int left = 0;
for(int right = 0; right < nums.length; right++){
while(nums[right] - nums[left] > mid){
left++;
}
count += right - left;
}
if(count >= k){
end = mid;
}else {
begin = mid + 1;
}
}
return begin;
}
}
#Coding一小時,Copying一秒鐘。留個言點個讚唄,謝謝你#