sort K-SortedArray

Tiffany ·2024년 4월 16일
0

AlgoExpert

목록 보기
20/20


import heapq

def sortKSortedArray(array, k):
    index = 0
    heap = array[:k+1] # 0~k
    heapq.heapify(heap)
    
    for i in range(k+1, len(array)):
        array[index] = heapq.heappop(heap)
        index += 1
        heapq.heappush(heap, array[i])
        
    while heap:
        array[index] = heapq.heappop(heap)
        index += 1
    
    return array
profile
Love what you do and don't quit.

0개의 댓글