Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- min heap, k elements. 可能是最简单的 hard 题目.
- from heapq import heappush, heappop
- class Solution:
- def KthInArrays(self, arrays, k):
- # init, min heap, negative.
- h = []
- for A in arrays:
- for v in A:
- if not h or len(h) <= k or v > h[0]:
- heappush(h, v)
- if len(h) > k:
- heappop(h)
- return h[0]
Advertisement
Add Comment
Please, Sign In to add comment