719. Find K-th Smallest Pair Distance
Related problem: #378, # 668. The difference is how to count how many smaller. To count quickly, you need to:
- Sort the array first.
- Do a while loop within a for loop; Each time the element that just gives enough distance to the one that in the outer loop; For next iteration, start looking from the last element found to save time.
Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B.
Example 1:
1 | Input: |
Note:
2 <= len(nums) <= 10000
.0 <= nums[i] < 1000000
.1 <= k <= len(nums) * (len(nums) - 1) / 2
.
Soulution:
1 | class Solution: |