Skip to content

Instantly share code, notes, and snippets.

@jason-curtis
Created January 21, 2020 23:25
Show Gist options
  • Save jason-curtis/4103400564578a5a312cca303238784f to your computer and use it in GitHub Desktop.
Save jason-curtis/4103400564578a5a312cca303238784f to your computer and use it in GitHub Desktop.
import heapq
# you have to create the list yourself
heap = [some list]
# heapq will modify your list in place!
# Organize the heap as a heap.
# Worst case time complexity: O(n log(n))
# Average time complexity: O(1)
heapq.heapify(heap)
# Add a value and reorganize the heap. O(log n)
heapq.push(heap, value)
# Pop the smallest value and reorganize the heap O(1)
smallest_value = heapq.pop(heap)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment