我正在python 3中使用如下^{priority queue
from queue import PriorityQueue
class PqElement(object):
def __init__(self, value: int):
self.val = value
#Custom Compare Function (less than or equsal)
def __lt__(self, other):
"""self < obj."""
return self.val > other.val
#Print each element function
def __repr__(self):
return f'PQE:{self.val}'
#Usage-
pq = PriorityQueue()
pq.put(PqElement(v)) # Add Item - O(Log(n))
topValue = pq.get() # Pop top item - O(1)
topValue = pq.queue[0].val # Get top value - O(1)
pqSize = pq.qsize() # Provide Queue Size - O(1)
isEmpty = pq.empty() # Is PQ is empty
我喜欢用O(log(n))搜索和删除优先级队列中的项目
我想知道有没有办法做到这一点
有人能帮忙吗
目前没有回答
相关问题 更多 >
编程相关推荐