Last active
May 2, 2020 12:17
-
-
Save jonathanagustin/3b7405119550a49809f95c865dbaecf7 to your computer and use it in GitHub Desktop.
Python Max Heap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import heapq | |
| class Neg(): | |
| def __init__(self, x): | |
| self.x = x | |
| def __cmp__(self, other): | |
| return -cmp(self.x, other.x) | |
| def maxheappush(heap, item): | |
| heapq.heappush(heap, Neg(item)) | |
| def maxheappop(heap): | |
| return heapq.heappop(heap).x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment