Python
Warning
This post is more than a year old. Information may be outdated.
Tip
Special thanks to Ishu Agrawal
Heap
Heaps are complete binary trees where the value of each node must be no greater than (or less than) the value of its child nodes.

- Python only supports Min Heaps
import heapqheapq.heapify(arr)heapq.heappop(arr)heapq.heappush(arr, x)heapq.nsmallest(k, arr, key=func)returns a list with theksmallest elements in the iterablearrbased on a comparator functionfunc- Runtime: $O(N \log k)$
heapq.nlargest(k, arr, key=func)returns a list with theklargest elements in the iterablearrbased on a comparator functionfunc- Runtime: $O(N \log k)$
| Operation | Runtime |
|---|---|
| Find min/max | $O(1)$ |
| Search | $O(n)$ |
| Insert | $O(\log n)$ |
| Remove | $O(\log n)$ |
| Heapify Array | $O(n)$ |
List Offsetting
You can offset with Python's enumerate function with list splitting.
enumerate(nums[offset::])
Dictionary
Alphanumeric Testing
c.isalnum()
Making 2d Arrays
- using
visited = [[False] * len(image[0])] * len(image)will not work- the rows will share the same memory and change in one will reflect on the others
- use
arr = [[0 for i in range(cols)] for j in range(rows)]
Backlinks (15)
- blank=True vs null=True in Django
- Migrating Project Aldehyde to FlightControl (February 24誠鉉)
- Daniele Romanini et al. PyVertical
- Scala
- Grammarly Work Note 2023-06-07
- Render.com
- Heap (Computer Systems)
- Person 1E6ABA
- Mathematics under The Library of Babel
- Higher-Level Languages and Their Speeds
- Real Exams
- Get Job Done
- 1046 Last Stone Weight
- 0001 Two Sum
- Coding Tests