Skip to content

Instantly share code, notes, and snippets.

@jdunck
Created March 13, 2019 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdunck/5212e962da87699d513fe2d6c8a30cd2 to your computer and use it in GitHub Desktop.
Save jdunck/5212e962da87699d513fe2d6c8a30cd2 to your computer and use it in GitHub Desktop.
Min/Max heap for python
import heapq
class Heap(object):
def __init__(self, max=False):
self.max = max
self.data = []
@property
def delta(self):
return -1 if self.max else 1
def push(self, item):
heapq.heappush(self.data, self.delta * item)
def pop(self):
return heapq.heappop(self.data) * self.delta
def top(self):
return self.delta * self.data[0]
def __len__(self):
return len(self.data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment