Skip to content

Commit 967353b

Browse files
authored
Update README_EN.md
1 parent 111cd72 commit 967353b

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_EN.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,43 @@ function smallestDistancePair(nums: number[], k: number): number {
220220
}
221221
```
222222

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

225262
<!-- solution:end -->

0 commit comments

Comments
 (0)