leetcode
Kadane's algorithm
- maximum subarray
- best time to buy & sell stocks (I, II, III, IV)
Maximun blah blah blah
subarray
- contiguous array
for finding subarray containing equal amount of elements, use
minus plus
. Dominus
for A element, and doplus
for B element. They will create two points with same Y-axis value (which means they eliminates the differences between them) - Maximum Size Subarray Sum Equals k
Bucket sort: sort in linear time. usually the data range is known.
Substring search & match
- Implement strStr(): use KMP algorithm (read this post)
Sliding windows:
- find a valid window (move right pointer first, minimize the window size by moving left forward), update result (with hashmap's help, i.e. calculate count, etc)
- make window invalid,
- and repeat
Dynamic programming
- DFS + memorization (top down)
- use state transformation formula (bottom up)
Depth first search + memorization
- frog jump: DFS + memorization, fail quickly!
- todo: read other people's solution
Breadth first search (one-end, two-end, or multiple-end search)
- one-end: tree
- two-end: know
start
andend
, such asWord Laddar
Graph
- check cycle: union find
- initialize array with
val = -1
- if
find(a)
equals tofind(b)
, it meansa
andb
are connected
- initialize array with
- find order: topological sort - Kahn's algorithm
- find start node with no incoming edges, and add them to a set
- start search from the set, check each of its neighbors.
- for each neighbor, remove the edge between itself and parent.
- if it has no incoming edges, add it to result set
L ← Empty list that will contain the sorted elements S ← Set of all nodes with no incoming edges while S is non-empty do remove a node n from S add n to tail of L for each node m with an edge e from n to m do remove edge e from the graph if m has no other incoming edges then insert m into S if graph has edges then return error (graph has at least one cycle) else return L (a topologically sorted order)
Design
- LRU Cache: write a separate
LinkedList
constructor, split them to decrease the complexity.
Greedy
Matrix & array
- in-place update:
- use
bit
,bitwise
operation
- use