Skip to content

Commit d2dd3ab

Browse files
authored
feat: add solution js lc problem no. 0719
1 parent 957fb1e commit d2dd3ab

File tree

1 file changed

+31
-0
lines changed
  • solution/0700-0799/0719.Find K-th Smallest Pair Distance

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
const n = nums.length;
9+
let left = 0,
10+
right = nums[n - 1] - nums[0];
11+
12+
while (left < right) {
13+
let mid = Math.floor((left + right) / 2);
14+
let count = 0, i = 0;
15+
16+
for (let j = 0; j < n; j++) {
17+
while (nums[j] - nums[i] > mid) {
18+
i++;
19+
}
20+
count += j - i;
21+
}
22+
23+
if (count >= k) {
24+
right = mid;
25+
} else {
26+
left = mid + 1;
27+
}
28+
}
29+
30+
return left;
31+
}

0 commit comments

Comments
 (0)