serega1112

786 - heap

Dec 17th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.40 KB | None | 0 0
  1. class Solution:
  2.     def kthSmallestPrimeFraction(self, A: List[int], K: int) -> List[int]:
  3.         n = len(A)
  4.         h = [(A[p]/A[n-1], p, n-1) for p in range(n-1)]
  5.         heapq.heapify(h)
  6.        
  7.         while K:
  8.             _, p, q = heapq.heappop(h)
  9.             if p < q - 1:
  10.                 heapq.heappush(h, (A[p]/A[q-1], p, q - 1))
  11.             K -= 1
  12.            
  13.         return [A[p], A[q]]
Advertisement
Add Comment
Please, Sign In to add comment