Advertisement
serega1112

719 - heap

Dec 19th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.43 KB | None | 0 0
  1. class Solution:
  2.     def smallestDistancePair(self, nums: List[int], k: int) -> int:
  3.         nums.sort()
  4.        
  5.         h = [(abs(nums[i] - nums[i+1]), i, i + 1) for i in range(len(nums) - 1)]
  6.         heapq.heapify(h)
  7.        
  8.         while k:
  9.             cur, i, j = heapq.heappop(h)
  10.             if j + 1 < len(nums):
  11.                 heapq.heappush(h, (abs(nums[i]-nums[j+1]), i, j+1))
  12.             k -= 1  
  13.         return cur
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement