def sift_down(array, index):
left = index*2+1
right = index*2+2
small = index
if left<len(array) and array[small]>array[left]:
small = left
if right<len(array) and array[small]>array[right]:
small = right
if small!=index:
array[small], array[index] = array[index], array[small]
sift_down(array, small)
def pop(self):
res = self.array[0]
self.array[0], self.array[-1] = self.array[-1], self.array[0]
self.array.pop()
self.sift_down(self.array,0)
return res
def build_heap(arr):
for i in range(len(arr)/2-1, -1, -1)
sift_down(arr)
class Solution(object):
def kSmallest(self, array, k):
"""
input: int[] array, int k
return: int[]
"""
# write your solution here
import heapq
result = []
heapq.heapify(array)
for i in range(min(k,len(array))):
result.append(heapq.heappop(array))
return result
def kSmallest2(array, k):
if not array:
return array
if k>=len(array):
return array
res = [-elem for elem in array[0:k]]
heapq.heapify(res)
for i in range(k,len(array)):
if array[i] < -res[0]
heapq.heappop(res)
heapq.heappush(res, -array[i])
return [-elem for elem in res]
def topKFrequentPQ(nums, k):
# 统计频率 O()
freq_hash = dict() #0 frequency, 1 number
for n in numbers:
if n in freq_hash:
freq_hash[n]+=1
else:
freq_hash[n] =1
# 末尾淘汰 n*2*O(logk)
heap=[]
for n, f in freq_hash.items():
# items()返回的是列表对象,iteritems()返回的是iterator对象
heapq.heappush(heap,(f,n))
if len(heap)>k:
heapq.heappop(heap)
# 调换顺序 O(klogk)
while len(heap)!=0:
ret.append(heapq.heappop(heap)[1])
ret.reverse()
return ret
def mergek(arrays):
if not arrays:
return None
heap = []
for i in range (len()):
if len(arrays[i]):
heap.append((arrays[i][0], i, 0))
heapq.heapify(heap)
result=[]
while heap:
val, index_array, index_element = heapq.heappop(heap)
result.append(val)
if index_element+1<len(arrays[index_array]):
heapq.heappush(heap, (arrays[index_array][index_element+1],
index_array, index_element+1))
return result
def merge_two_streams(A, B):
INVALID = sys.maxsize
streams = (A, B)
cache = [(INVALID, 0), (INVALID, 1)]
for i in [0, 1]:
try:
cache[i] = (next(streams[i]), i)
except StopIteration:
cache[i] = (INVALID, i)
while cache:
min_val, index = min(cache)
if min_val == INVALID:
break
try:
cache[index] = (next(streams[index]), index)
except StopIteration:
cache[index] = (INVALID, index)
yield min_val
def merge_three_streams(A, B, C):
INVALID = sys.maxsize
streams = (A, B, C)
cache = [(INVALID, 0), (INVALID, 1), (INVALID, 2)]
for i in [0, 1, 2]:
try:
cache[i] = (next(streams[i]), i)
except StopIteration:
cache[i] = (INVALID, i)
while cache:
min_val, index = min(cache)
if min_val == INVALID:
break
try:
cache[index] = (next(streams[index]), index)
except StopIteration:
cache[index] = (INVALID, index)
yield min_val
from heapq import heapify, heappop, heappush
def merge_k_streams(streams):
cache, k = [], len(streams)
for i in range(k):
try:
cache.append((next(streams[i]),i))
except StopIteration:
pass
heapify(cache)
while cache:
min_val, index = heappop(cache)
try:
heappush(cache, (next(streams[index]), index))
except StopIteration:
pass
yield min_val
def streaming(nums):
for num in nums: yield num
if __name__ == "__main__":
print([num for num in merge_two_streams(streaming([1,3,5]),streaming([2,4,6]))])
print([num for num in merge_three_streams(streaming([1,4,7]),streaming([2,5,8]),streaming([3,6,9]))])
print([num for num in merge_k_streams([streaming(nums) for nums in [[1,3,5,7],[2,4,6,8],[1,4,7,10],[2,5,8,11]]])])