728x90

힙은 최댓값, 최솟값을 찾는 연산을 빠르게 해줌 -> 환전이진트리를 사용

 

import heapq 필요

 

  • heapq.heappush(heap, item) : item을 heap에 추가
  • heapq.heappop(heap) : heap에서 가장 작은 원소를 pop & 리턴. 비어 있는 경우 IndexError가 호출됨. 
  • heapq.heapify(x) : 리스트 x를 즉각적으로 heap으로 변환함 (in linear time, O(N) )

 

 

최대 힙 만들기 

-> for item in heap_items:

       heapq.heappush(max_heap, (-item, item))

728x90

+ Recent posts