arrays
- Hashing
- Hashset
- Two pointers
- prefix and sufix
- Kadane's Algorithm
- sorted - binary search
dp Recursion -> Memoization -> Tabulation -> Space Optimization
Memoization (recursion) = Top-down = main problem -> base case
Steps:
- create dp[n+1] array initialized to -1
- if(dp[n] != -1) -> return dp[n] // value is already computed
- else -> use recursive relation to compute dp[n] -> at the end, store the result
Tabulation = Bottom-up = base case -> main problem
Steps:
- declare dp[n+1] array
- initialize the base case -> dp[0] and/or dp[1]
- traverse array -> every index -> dp[i] = dp[i-1] + dp[i-2]
Space Optimization
Steps:
- If additional space is not required, use variables dp[i-1] -> prev1 dp[i-2] -> prev2
sorting
- selection sort - find min and swap
- bubble sort - swap locally by moving largest element to the end
- quick sort - pivot (partition) -> 1st element; i -> more than pivot; j -> less than pivot; swap
- Merge sort - merge two sorted array
- insertion sort - 1st element is sorted; every time 1 element is inserted into sorted arr
- heap sort -
- selection sort
search
- linear search
- binary search - low, mid, high
Linked List:
- Use pointers
- Use dummy nodes
graph:
- dfs - stack
- bfs - queue (Shortest Path)
- directed acyclic graph (DAG) - topological sorting - dfs -> reverse pop
adjacent matrix adjacent list - for every vertex - all edges
shortest path in graph single source shortest path
tree: Solve by recursion
- binary tree (1 parent - 2 child) hieght: max(height(left), height(right)) + 1 count number of edges node to leaf depth = level = node to root
BT -> Heap BST -> AVL -> BTT -> 2-3 tree (B Tree)
AVL
- Balanced BST
- Balancing Factor (BF) = Height of Left Subtree - Height of Right Subtree
- AVL is balanced if BF = 0, 1 or -1
Insertion of node Deletion of node
Spanning Tree - Convert Graph -> Tree
Preoder - root, left, right Inorder - left, root, right Postorder - left, right, root
Travelling Salesman Problem (TSP) Knapsack Problem Job Assignment Problem Divide and Conquer Decrease and Conquer - divide into two; discard one piece Fake Coin Problem
heap - find kth smallest elem
- tree (complete BT)
- parental domination p -> left child = 2p+1; right child = 2p+2 last parent -> (n/2)-1 ; n = # nodes min heap; max heap
Floyd Warshall Algo
- All pair shortest path
- Generate shortestPathMatrix
- Intermediate node - k
For k = 0 to n – 1
For i = 0 to n – 1
For j = 0 to n – 1
Distance[i, j] = min(Distance[i, j], Distance[i, k] + Distance[k, j])
where i = source Node, j = Destination Node, k = Intermediate Node
---- BD
Dijkstra's Algo
- Single source shortest path
- Unweighted graph
- Greedy method
- Create shortest path tree (SPT) with given source as root
- maintain 2 set one set contains vertices included in SPT another set contains vertices not included in SPT
- {0 INF INF INF INF INF} -> update this with weight
- {0 ... } -> SPT
Bellman Ford
- Single source shortest path
- Weight graph / Detect Negative edges
- distance array and edges list
- A -> B = 3 ; d(A) + 3 = d(B)
- Iterate thro all the edges (v-1) times
- if relaxation for vth time is possible then negative weight cycle is detected
Minimum Spanning Tree (PK)
- Prim's Algo
- pick any start vertex
- identify fringe vertex (vertex connected to the cur vertex)
- pick the smallest edge
- add to the tree if it does not form cycle
- Kruskal's Algo
-
sort the edges
-
pick smallest edge if no cycle, then add the edge
-
(v-1) edges
-
Count Number of Islands
-
Word Search - Backtracking
-
All the paths from source to destination = dfs + backtracking