Pπππ»π πΎπ Sππ ππππ Tips
1. If an input array is sorted then
a. Binary search O(log n)
b. Two pointers O(n)
2. If asked for all permutations/subsets then
a. Backtracking (First preference)
b. Breadth First Search.
3. If given a tree or graphs then
a. DFS (Preorder, Inorder, Postorder): O(n)
b. BFS (Level Order): O(n)
4. If Binary search tree:
a. - Left < Cur < Right: O(log n)
b. - Inorder Traversal visits the nodes in ascending (sorted) order: O(n
5. If given a linked list then
a. Two pointers
6. If recursion is banned then
a. Convert to iterative solution using Stack
7. If must solve in-place then
a. Swap corresponding values
b. Store one or more different values in the same pointer
8. If asked for maximum/minimum subarray/subset/options then
a. Dynamic programming
9. If asked for top/maximum/minimum/closest/least K items then
a. Heap
10. If asked for common strings among a set of strings
a. Hash Map
b. Trie
11. If we need to search/manipulate a bunch of strings:
a. Trie
12. For a problem involving arrays, if there exists a solution in O(n^2) time and O(1) space, there must exist two other solutions:
a. Use a HashMap or a Set for O(n) time and O(n) space
b. Sort input for O(n log n) time and O(1) space
13. Else
a. Use a HashMap or a Set for O(n) time and O(n) space
Sort input for O(n log n) time and O(1) space
No comments:
Write comments