Skip to content
LC-0719 Hard LeetCode

719. Find K-th Smallest Pair Distance

Read the full problem statement on LeetCode.
Difficulty: hard Acceptance: 46% Topics: Array, Two Pointers, Binary Search, Sorting
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(nlogn + nlogw), n = len(nums), w = max(nums)-min(nums)
# Space: O(1)

class Solution(object):
    def smallestDistancePair(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        # Sliding window solution
        def possible(guess, nums, k):
            count, left = 0, 0
            for right, num in enumerate(nums):
                while num-nums[left] > guess:
                    left += 1
                count += right-left
            return count >= k

        nums.sort()
        left, right = 0, nums[-1]-nums[0]+1
        while left < right:
            mid = left + (right-left)/2
            if possible(mid, nums, k):
                right = mid
            else:
                left = mid+1
        return left

Solution from kamyu104/LeetCode-Solutions · MIT