Loading section...
Lazy Deletion: Removing Arbitrary Elements from a Heap
Concepts: pyLazyHeap, pyReprioritize, pyCancellableQueue
This is one of the most underrated heap techniques in competitive programming and production systems. heapq gives you no way to efficiently remove an arbitrary element. heapq.remove() exists but it is O(n) — it does a linear scan. For a priority queue where elements can be cancelled or reprioritized, O(n) deletion makes the whole data structure O(n²) in the worst case. The lazy deletion trick brings it back to O(log n) amortized. It is used in cache eviction, scheduler cancellation, and priority queue reprioritization. The Lazy Deletion Pattern Reprioritization: Change Key in O(log n)