algorithm cheat sheet in Python summarised by data types
- HashMap
- Set
- Deque
- Heap
- LRU Cache
- MRU Cache
- LFU Cache
- Priority Cache
- Message Queue Scheduled
- Rate Limiter
count combination
substring(shortest, longest, len, min, max) -> greedy, sliding window
- Substring Longest Palindromic
palindromic
- Combination Validate Parentheses
left_min, left_max
string(index, count, last, match) -> Hashmap
- Group by Anagram
{sorted_string: list}
- Substring Longest with Char Replacement
{char: count}
- Substring Longest without Repeating Characters
{char: last}, seen[c] >= i
- Substring Check Target Anagram
{char: count}
- Splits Count Equal Unique Chars
{char: first}, {char: last}
- Splits Char Unique Part
{char: last}
subsequence, combination(count, match, min, max) -> dynamic programming
- Splits Count by Dict
if match_condition: dp[n] += dp[n-1] or dp[n-2]
- Splits Count Match by Dict
dp[i] = True if any(dp[n-len(w)] and s[i-len(w):i] == w)
2D Dynamic Programming O(M*N) time, O(M or N or M*N) space
- multiple relations and multiple strings
two subsequences, subsequence match -> 2d dynamic programming
combination(generate) -> backtracking, combination(dictionary) -> DFS
sorted search, search peak -> binary search
- Rotated Sorted Array Search
- Rotated Sorted Array Min
target is converged boundary
- Search Element Peak
left on uphill, right on downhill
- Subsequence Fixed Size Max Min Gap
binary search on possible gap
subarray(max, min, longest, except self) -> greedy
- Subarray Max Product
rolling vals, min, max
- Subarray Min Size Sum
rolling res, sliding window
- Search Min Max Diff After Operation
all possible cases, sort pair iterate
- Subarray Longest Consecutive
consecutive, set
- Subarray Product Except Self
async except-self product
- Subarray Addition Multiple
lazy rolling addition
- Consecutive Int Missing
sum diff
- Triplet Check Increasing
anchor if elif else
subarray(area, remove, longest, shortest) -> sliding window
- Subarray Max Containable Area
shrinking width updating min height
- Subarray Total Patch Containable
update the bar on the smaller side
- Subarray Shortest Remove to Sort
shortest window
- Subarray Longest Sum Within K
- Triplets Count Increasing Sum Within K
- Sorted Array Two Indexes Sum to K
array(index, count, last) -> Hashmap
- Search Two Indexes Sum to K
{match_condition: index}
- Subarray Count Sum to K
{sum: count}``sum[i:j] = sum[:j] - sum[:i]
- Subarray Count Sum Divisible by K
{sum%k: count}
stream sliding window max, rectangle area -> stack
- Stream Sliding Window Maxes [index of max until i]
- Subarray Max Area Rectangle
[(left, height)]
stream k, kth, median, subgroups consecutive -> heap
- Search K Elements Closest to Origin
- Stream Find Median min max heaps
- Stream Sliding Window Median min max heaps, lazy removal
- Subgroups Fixed Size Check Consecutive
- (Triplet) Search Three Indexes Sum to K
negative positive combinations
subsequence or combination(fewest, sum to, longest) -> dynamic programming
- Combination Count Sum To K
dp[i] += dp[i-coin]
- Subsequence Longest Increasing
dp[j] = max(dp[i]+1 if nums[i] < nums[j])
- Two Arrays Min Path Cost
from_red, from_blue dp
- Combination Fewest Elements Sum to K
dp[a] = min(dp[a], 1 + dp[a - c])
subsequence(average)
- Subsequence Search Equal Average
dp[i] |= {s+a for s in dp[i+1] if s+a <= HALF}
combination(generate) -> backtracking
sorted search, search peak -> binary search
shortest path, shortest distance, edge areas -> BFS
count longest path
longest path, target sequence, edge areas -> DFS
n << 1 <=> n * 2
n >> 1 <=> n // 2
n & 1 <=> n % 2
range(32) or range(31, -1, -1)
bit base(conversion, calculation)
undirected set
directed cycle, paths
matrix count groups