Last active
March 8, 2026 18:06
-
-
Save JaDogg/91c761c360ef9b90cc0be6c19bacf777 to your computer and use it in GitHub Desktop.
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
| from collections import Counter, OrderedDict, ChainMap, defaultdict | |
| from functools import lru_cache | |
| from sortedcontainers import SortedList, SortedSet, SortedDict | |
| import heapq | |
| class MaxHeapObj(object): | |
| def __init__(self, val): self.val = val | |
| def __lt__(self, other): return self.val > other.val | |
| def __eq__(self, other): return self.val == other.val | |
| def __str__(self): return str(self.val) | |
| class MinHeap(object): | |
| def __init__(self): self.h = [] | |
| def heappush(self, x): heapq.heappush(self.h, x) | |
| def heappop(self): return heapq.heappop(self.h) | |
| def __getitem__(self, i): return self.h[i] | |
| def __len__(self): return len(self.h) | |
| @classmethod | |
| def heapify(cls, arr): | |
| heapq.heapify(arr) | |
| mh = MinHeap() | |
| mh.h = arr | |
| return mh | |
| class MaxHeap(MinHeap): | |
| def heappush(self, x): heapq.heappush(self.h, MaxHeapObj(x)) | |
| def heappop(self): return heapq.heappop(self.h).val | |
| def __getitem__(self, i): return self.h[i].val | |
| @classmethod | |
| def heapify(cls, arr): | |
| arr = [MaxHeapObj(x) for x in arr] | |
| heapq.heapify(arr) | |
| mh = MaxHeap() | |
| mh.h = arr | |
| return mh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment