class PriorityQueue(object): """PriorityQueue: a partially ordered queue with methods to enqueue items in priority order and to access and dequeue its highest priority item. Item pairs are stored in a binary min heap for its efficient operations.""" def __init__(self): """Initialize this priority queue.""" # Initialize new binary min heap to store items in this priority queue self.heap = BinaryMinHeap() def __repr__(self): """Return a string representation of this priority queue.""" return 'PriorityQueue({} items, front={})'.format( self.length(), self.front()) def is_empty(self): """Return True if this priority queue is empty, or False otherwise.""" return self.heap.is_empty() def length(self): """Return the number of items in this priority queue.""" return self.heap.size() def enqueue(self, item, priority): """Insert the given item into this priority queue in order according to the given priority.""" # New item to enqueue new_item = (priority, item) # Insert given item into heap in order according to given priority self.heap.insert(new_item) def front(self): """Return the item at the front of this priority queue without removing it, or None if this priority queue is empty.""" if self.heap.size() == 0: return None # Return minimum item from heap return self.heap.get_min() def dequeue(self): """Remove and return the item at the front of this priority queue, or raise ValueError if this priority queue is empty.""" if self.heap.size() == 0: raise ValueError('Priority queue is empty and has no front item') # Remove and return minimum item from heap self.heap.delete_min() def push_pop(self, item, priority): """Remove and return the item at the front of this priority queue, and insert the given item in order according to the given priority. This method is more efficient than calling dequeue and then enqueue.""" if self.heap.size() == 0: raise ValueError('Priority queue is empty and has no front item') # New item to replace replaced_item = (priority, item) # Replace and return minimum item from heap self.heap.replace_min(replaced_item)
def get_suggestion(org_handle, handle_array, k=2) -> [str]: assert len(handle_array) >= k suggestion_min_heap = BinaryMinHeap() handle_set = set(org_handle) # O(c) for handle in handle_array: # O(n) if suggestion_min_heap.size() < k: curr_score = get_score(handle_set, handle) # O(c) suggestion_min_heap.insert((handle, curr_score)) # O(k) elif suggestion_min_heap.get_min()[1] > len( handle): # Check whether to get the score or not continue else: curr_score = get_score(handle_set, handle) # O(c) if curr_score > suggestion_min_heap.get_min()[1]: suggestion_min_heap.replace_min((handle, curr_score)) return [x[0] for x in suggestion_min_heap.items] # O(k)
class PriorityQueue(object): """PriorityQueue: a partially ordered queue with methods to enqueue items in priority order and to access and dequeue its highest priority item. Item pairs are stored in a binary min heap for its efficient operations.""" def __init__(self): """Initialize this priority queue.""" # Initialize new binary min heap to store items in this priority queue self.heap = BinaryMinHeap() def __repr__(self): """Return a string representation of this priority queue.""" return 'PriorityQueue({} items, front={})'.format(self.size(), self.front()) def is_empty(self): """Return True if this priority queue is empty, or False otherwise.""" return self.heap.is_empty() def length(self): """Return the number of items in this priority queue.""" return self.heap.size() def enqueue(self, item, priority): """Insert the given item into this priority queue in order according to the given priority.""" # TODO: Insert given item into heap in order according to given priority # ... # Note that I probably could have done this with a list instead self.heap.insert((priority, item)) def front(self): """Return the item at the front of this priority queue without removing it, or None if this priority queue is empty.""" if self.size() == 0: # Basically, if the queue is empty, then just retirn that theres no front return None # TODO: Return minimum item from heap # ... front_item = self.heap.get_min() return front_item[1] def dequeue(self): """Remove and return the item at the front of this priority queue, or raise ValueError if this priority queue is empty.""" if self.size() == 0: raise ValueError('Priority queue is empty and has no front item') # TODO: Remove and return minimum item from heap # ... last_item = self.heap.delete_min() return last_item[1] def push_pop(self, item, priority): """Remove and return the item at the front of this priority queue, and insert the given item in order according to the given priority. This method is more efficient than calling dequeue and then enqueue.""" if self.size() == 0: raise ValueError('Priority queue is empty and has no front item') # TODO: Replace and return minimum item from heap # ... popped_item = self.heap.replace_min(priority, item) return popped_item[1]