File tree 2 files changed +41
-0
lines changed
2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 543
543
715|[ Range Module] ( ./0715-range-module.js ) |Hard|
544
544
717|[ 1-bit and 2-bit Characters] ( ./0717-1-bit-and-2-bit-characters.js ) |Easy|
545
545
718|[ Maximum Length of Repeated Subarray] ( ./0718-maximum-length-of-repeated-subarray.js ) |Medium|
546
+ 719|[ Find K-th Smallest Pair Distance] ( ./0719-find-k-th-smallest-pair-distance.js ) |Hard|
546
547
720|[ Longest Word in Dictionary] ( ./0720-longest-word-in-dictionary.js ) |Medium|
547
548
722|[ Remove Comments] ( ./0722-remove-comments.js ) |Medium|
548
549
724|[ Find Pivot Index] ( ./0724-find-pivot-index.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 719. Find K-th Smallest Pair Distance
3
+ * https://leetcode.com/problems/find-k-th-smallest-pair-distance/
4
+ * Difficulty: Hard
5
+ *
6
+ * The distance of a pair of integers a and b is defined as the absolute difference between a and b.
7
+ *
8
+ * Given an integer array nums and an integer k, return the kth smallest distance among all the
9
+ * pairs nums[i] and nums[j] where 0 <= i < j < nums.length.
10
+ */
11
+
12
+ /**
13
+ * @param {number[] } nums
14
+ * @param {number } k
15
+ * @return {number }
16
+ */
17
+ var smallestDistancePair = function ( nums , k ) {
18
+ nums . sort ( ( a , b ) => a - b ) ;
19
+ let left = 0 ;
20
+ let right = nums [ nums . length - 1 ] - nums [ 0 ] ;
21
+
22
+ while ( left < right ) {
23
+ const middle = Math . floor ( ( left + right ) / 2 ) ;
24
+ let count = 0 ;
25
+ let j = 0 ;
26
+
27
+ for ( let i = 0 ; i < nums . length ; i ++ ) {
28
+ while ( j < nums . length && nums [ j ] - nums [ i ] <= middle ) j ++ ;
29
+ count += j - i - 1 ;
30
+ }
31
+
32
+ if ( count < k ) {
33
+ left = middle + 1 ;
34
+ } else {
35
+ right = middle ;
36
+ }
37
+ }
38
+
39
+ return left ;
40
+ } ;
You can’t perform that action at this time.
0 commit comments