40 minimum number k

Title: input integer n, to find the minimum number k therein.

import heapq
def get_min_k(arry,k):
    max_heap = []
    l = len(arry)
    if l<1 or k<1 or k>l:
        return None
    for arr in arry:
        arr = -1*arr
        if len(max_heap)<k:
            heapq.heappush(max_heap,arr)
        else:
            heapq.heappushpop(max_heap,arr)

    res = [-1*i for i in max_heap]
    return res

Note: Use a large heap to achieve the top, because the top of the heap in python only a small library, so the list of integers can be negated with a small top of the heap library implements a large pile top, the final results will then negated.

Guess you like

Origin www.cnblogs.com/kingshine007/p/11360859.html