Data structures
Union find
Shortcut: ds.uf
Time complexity: O(α(n))
Memory complexity: O(n)
|
|
Refer to: UVA11503
Segment tree
Segment tree support online update and query For range update, lazy propagation is needed
Shortcut: ds.st
Time complexity:
- Build: O(nlogn)
- Query: O(log n)
- Update: O(log n)
Memory: O(n)
|
|
- Lazy operation: push()
Refer to: UVA11402
- Segment tree on interval instead of update range of value.
2D Segment tree
Shortcut: ds.st2d
|
|
Refer to: 308. Range Sum Query 2D - Mutable
Refer to: UVA11297
Fenwick tree
Shortcut: ds.ft
Time complexity:
- Build: O(nlogn), but calling adjust on each element.
- Query: O(log n)
- Update: O(log n)
Memory: O(n) - less memory compare to segment tree
|
|
Refer to UVA12086
2D Fenwick tree
|
|
Refer to Range Sum Query 2D - Mutable
Heap
Not much used, can be replaced by PriorityQueue in most setting.
Update element in PriorityQueue replaced by either delete O(N) and insert again O(logN) or being lazy and check if the element pop from PriorityQueue is the same as the latest record.
Application: It is inferor to linear scan if the scan is small. Refer to Longest Repeating Character Replacement, which linear scan perform much faster.
TODO: There is currently no bubbleUp and bubbleDown implementation in my implementation of heap.
Refer: Kth Largest Element in a Stream
Quick select
Most Top K problem can also be solved by quick select, which have a better time complexity of O(N)
|
|
Refer: Kth Largest Element in an Array
Monotonic stack / heap
Monotonic heap
Usually used for querying max/min in sliding windows
Refer: Sliding Window Maximum Refer: Max Value of Equation
Monotonic stack
Search for previous min or next min.
Refer: Largest Rectangle in Histogram
Refer: Maximal Rectangle
Make use of Largest Rectange in Histogram
Refer: Sum of Subarray Minimums