Skip to content

Commit 111cd72

Browse files
authored
Update README.md
1 parent f8d7187 commit 111cd72

File tree

1 file changed

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

1 file changed

+37
-0
lines changed

solution/0700-0799/0719.Find K-th Smallest Pair Distance/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,43 @@ function smallestDistancePair(nums: number[], k: number): number {
226226
}
227227
```
228228

229+
#### JavaScript
230+
231+
```js
232+
/**
233+
* @param {number[]} nums
234+
* @param {number} k
235+
* @return {number}
236+
*/
237+
function smallestDistancePair(nums, k) {
238+
nums.sort((a, b) => a - b);
239+
const n = nums.length;
240+
let left = 0,
241+
right = nums[n - 1] - nums[0];
242+
243+
while (left < right) {
244+
const mid = (left + right) >> 1;
245+
let count = 0,
246+
i = 0;
247+
248+
for (let j = 0; j < n; j++) {
249+
while (nums[j] - nums[i] > mid) {
250+
i++;
251+
}
252+
count += j - i;
253+
}
254+
255+
if (count >= k) {
256+
right = mid;
257+
} else {
258+
left = mid + 1;
259+
}
260+
}
261+
262+
return left;
263+
}
264+
```
265+
229266
<!-- tabs:end -->
230267

231268
<!-- solution:end -->

0 commit comments

Comments
 (0)