Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def smallestDistancePair(self, nums: List[int], k: int) -> int:
- nums.sort()
- h = [(abs(nums[i] - nums[i+1]), i, i + 1) for i in range(len(nums) - 1)]
- heapq.heapify(h)
- while k:
- cur, i, j = heapq.heappop(h)
- if j + 1 < len(nums):
- heapq.heappush(h, (abs(nums[i]-nums[j+1]), i, j+1))
- k -= 1
- return cur
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement