19 lines
405 B
Python
19 lines
405 B
Python
![]() |
import heapq
|
||
|
from typing import List, Tuple, TypeVar
|
||
|
|
||
|
T = TypeVar("T")
|
||
|
|
||
|
|
||
|
class PriorityQueue:
|
||
|
def __init__(self):
|
||
|
self.elements: List[Tuple[float, T]] = []
|
||
|
|
||
|
def empty(self) -> bool:
|
||
|
return not self.elements
|
||
|
|
||
|
def put(self, item: T, priority: float):
|
||
|
heapq.heappush(self.elements, (priority, item))
|
||
|
|
||
|
def get(self) -> T:
|
||
|
return heapq.heappop(self.elements)[1]
|