Data Structures and Algorithms Cheat Sheet: by Via
Data Structures and Algorithms Cheat Sheet: by Via
Stores data elements based on an sequential, most commonly 0 Stack Queue Deque Heap
based, index. Last In First First In Last Provides Ascending
Time Complexity Out Out first/last Order
● Indexing: Linear array: O(1), Dynamic array: O(1) push(val) offer(val) offer(val) offer(val)
● Search: Linear array: O(n), Dynamic array: O(n) pop() poll() poll() poll()
● Optimized Search: Linear array: O(log n), Dynamic array: O(log peek() peek() peek() peek()
n)
Implementation in Java:
● Insertion: Linear array: n/a, Dynamic array: O(n)
● Stack<E> stack = new Stack();
Bonus:
● Queue<E> queue = new LinkedList();
● type[] name = {val1, val2, ...}
● Deque<E> deque = new LinkedList();
● Arrays.sort(arr) -> O(n log(n))
● PriorityQueue<E> pq = new PriorityQueue();
● Collections.sort(list) -> O(n log(n))
● int digit = '4' - '0' -> 4
DFS & BFS Big O Notation
● String s = String.valueOf('e') -> "e"
Time Space
● (int) 'a' -> 97 (ASCII)
● new String(char[] arr) ['a','e'] -> "ae" DFS O(E+V) O(Height)
● (char) ('a' + 1) -> 'b' BFS O(E+V) O(Length)
● Character.isLetterOrDigit(char) -> true/false
V & E -> where V is the number of vertices and E is the number of
● new ArrayList<>(anotherList); -> list w/ items
edges.
● StringBuilder.append(char||String)
Height -> where h is the maximum height of the tree.
Length -> where l is the maximum number of nodes in a single level.
Linked List
Stores data with nodes that point to other nodes. DFS vs BFS
Time Complexity DFS BFS
● Indexing: O(n)
●Better when target is closer to ●Better when target is far from
● Search: O(n)
Source. Source.
● Optimized Search: O(n)
●Stack -> LIFO ●Queue -> FIFO
● Append: O(1)
●Preorder, Inorder, Postorder ●Level Order Search
● Prepend: O(1)
Search ●Goes wide
● Insertion: O(n)
●Goes deep ●Iterative
●Recursive ●Slow
HashTable
●Fast
Stores data with key-value pairs.
Time Complexity
● Indexing: O(1)
● Search: O(1)
● Insertion: O(1)
Bonus:
● {1, -1, 0, 2, -2} into map
HashMap {-1, 0, 2, 1, -2} -> any order
LinkedHashMap {1, -1, 0, 2, -2} -> insertion order
TreeMap {-2, -1, 0, 1, 2} -> sorted
● Set doesn't allow duplicates.
● map.getOrDefaultValue(key, default value)
BFS Impl for Graph BFS Impl. for Level-order Tree Traversal
public boolean connected(int[][] graph, int start, private void printLevelOrder(TreeNode root) {
int end) { Queue<TreeNode> queue = new LinkedList<>();
Set<Integer> visited = new HashSet<>(); queue.offer(root);
Queue<Integer> toVisit = new LinkedList<>(); while (!queue.isEmpty()) {
toVisit.enqueue(start); TreeNode tempNode = queue.poll();
while (!toVisit.isEmpty()) { print(tempNode.data + " ");
int curr = toVisit.dequeue();
if (visited.contains(curr)) continue; // add left child
if (curr == end) return true; if (tempNode.left != null) {
for (int i : graph[start]) { queue.offer(tempNode.left);
toVisit.enqueue(i); }
}
visited.add(curr); // add right right child
} if (tempNode.right != null) {
return false; queue.offer(tempNode.right);
} }
}
DFS Impl for Graph }
} }
private void backtrack(List<List<Integer>> list, }
List<Integer> tempList, int [] nums, int start){ }
list.add(new ArrayList<>(tempList));
for(int i = start; i < nums.length; i++){
// skip duplicates
if(i > start && nums[i] == nums[i-1])
continue;
// skip duplicates
tempList.add(nums[i]);
backtrack(list, tempList, nums, i + 1);
tempList.remove(tempList.size() - 1);
}
}
- Permutations
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
// Arrays.sort(nums); // not necessary
backtrack(list, new ArrayList<>(), nums);
return list;
}
private void backtrack(List<List<Integer>> list,
List<Integer> tempList, int [] nums){
if(tempList.size() == nums.length){
list.add(new ArrayList<>(tempList));
} else{
for(int i = 0; i < nums.length; i++){
// element already exists, skip
if(tempList.contains(nums[i])) continue;
// element already exists, skip
tempList.add(nums[i]);
backtrack(list, tempList, nums);
tempList.remove(tempList.size() - 1);