PL/Python

[자료구조][Python] heap의 구현 / heapq 모듈 / priority queue

SweetDev 2021. 8. 5. 15:15

 

정의를 알고싶다면 >> https://sweetdev.tistory.com/126

구현

heapq는 내장 모듈이어서 바로 사용 가능하다. 

문제는 heapq에서 제공하는 힙은 최소힙이여서, 최대 힙으로 쓰려면 살짝의 트릭을 써야 한다. 

 

PriorityQueue 구현하기

파이썬의 우선순위큐 구현은 PriorityQueue대신 heapq모듈을 쓰는것이 좋다. 

파이썬의 PriorityQueue 내부는 결국 heapq라서 heapq를 쓰는것이 훨씬 빠르기 때문이다!

 

 

[참고]

https://littlefoxdiary.tistory.com/3