File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
solution/0700-0799/0719.Find K-th Smallest Pair Distance Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -220,6 +220,43 @@ function smallestDistancePair(nums: number[], k: number): number {
220
220
}
221
221
```
222
222
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
+
223
260
<!-- tabs:end -->
224
261
225
262
<!-- solution:end -->
You can’t perform that action at this time.
0 commit comments