Created
January 21, 2020 23:25
-
-
Save jason-curtis/4103400564578a5a312cca303238784f to your computer and use it in GitHub Desktop.
This file contains 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 | |
# 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