Skip to content

Commit 3e92383

Browse files
authored
Update 719-find-k-th-smallest-pair-distance.js
1 parent 8d357cc commit 3e92383

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

719-find-k-th-smallest-pair-distance.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
function smallestDistancePair(nums, k) {
7+
nums.sort((a, b) => a - b)
8+
let l = 0, n = nums.length, r = nums[n - 1] - nums[0]
9+
10+
let res = 0
11+
while(l < r) {
12+
let cnt = 0, mid = l + ((r - l) >> 1)
13+
for(let i = 0, j = 0; i < n; i++) {
14+
while(j < n && nums[j] <= nums[i] + mid) j++
15+
cnt += j - 1 - i
16+
}
17+
if(cnt < k) l = mid + 1
18+
else r = mid
19+
}
20+
21+
return l
22+
}
23+
24+
// another
25+
126
/**
227
* @param {number[]} nums
328
* @param {number} k

0 commit comments

Comments
 (0)