实现一个优先级队列-2021护网行动面试题目

1.5实现一个优先级队列问题怎样实现一个按优先级排序的队列?并且在这个队列上面每次pop操作总是返回优先级最高的那个元素解决方案下面的类利用heapq模块实现了一个简单的优先级队列: import heapq class PriorityQueue: def __init__(self): self._queue = [] self._index = 0 def push(self, item, priority): heapq.heappush(self._queue, (-priority, self._index, item)) self._index += 1
pdf 文件大小:2.01MB