Advertisement
goodwish

543. Kth Largest in N Arrays

Nov 10th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.43 KB | None | 0 0
  1. min heap, k elements. 可能是最简单的  hard 题目.
  2.  
  3. from heapq import heappush, heappop
  4. class Solution:
  5.     def KthInArrays(self, arrays, k):
  6.         # init, min heap, negative.
  7.         h = []
  8.        
  9.         for A in arrays:
  10.             for v in A:
  11.                 if not h or len(h) <= k or v > h[0]:
  12.                     heappush(h, v)
  13.                 if len(h) > k:
  14.                     heappop(h)
  15.         return h[0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement