Runtime
- usually do not consider constants when analyzing the time complexity
- usually want to find the "family" of functions that most closely the growth in computational time
- e.g.:
O(2n)
, O(5n)
can be loosely said to belong the family of functions O(n)
- common convention to simply consider it an
O(n)
function and discard the constant
- however important to be careful of constants
- sometimes a sufficiently bad constant can increase runtime substantially
- In some specific cases one may want to consider optimizing algorithms to have a better constant
O(1)
- Constant time complexity
- e.g.:
- Hashmap lookup
- Finding and applying math formula
O(log(N))
- grows VERY slowly
- lookup by primary key in a relational database is log(N)
- many mainstream relational databases such as postgres use B-trees for indexing by default
- search in B-tree is log(N)
- e.g.:
- Binary search or variant
- Balanced binary search tree lookup
O(N)
- Linear time normally means looping through a linear data structure a constant number of times
- e.g.:
- Going through array/linked list
- Two pointers
- Some types of greedy
- Tree/graph traversal
- Stack/Queue
O(K log(N))
- Heap push/pop K times
- When you encounter problems that look for the "top K elements"
- you can usually solve them with pushing and popping to a heap, K times
- e.g.:
- K closest points
- merge K sorted lists
- Binary search K times
O(N log(N))
- Sorting
- The default sorting algorithm's expected runtime in all mainstream languages
- e.g. java uses a variant of merge sort for object sorting and a variant of quick sort for primitive type sorting
- Divide and conquer with a linear time merge operation
- Divide is normally
log(N)
, and if merge is O(N)
then the overall runtime is O(N log(N))
- e.g.:
- smaller numbers to the right
O(N^2)
- quadratic time
- Nested loops
- For small N < 1000, is not that bad for modern computers
- can probably pass most Leetcode tests with quadratic time for small Ns
- However, in an interview, solution usually has to do better than quadratic time to impress the interviewer
- e.g.:
- Many brute force solutions
- visiting each matrix entry
O(2^N)
- Grows very rapidly
- Often requires memoization to avoid repeated computations and reduce complexity
- e.g.:
- Combinatorial problems
- backtracking
O(N!)
- Grows insanely rapidly
- Only solvable by computers for small N
- Often requires memoization to avoid repeated computations and reduce complexity
- e.g.:
- Combinatorial problems
- backtracking
Keywords
Top k
How many ways..
Substring
- Sliding window
- Longest substring without repeating characters
Palindrome
- two pointers
- DFS
- DP
- Palindrome Partitioning 2
Tree
- shortest, level-order
- BFS
- Binary tree level-order traversal
- else
Parentheses
Subarray
X Sum
Max/longest sequence
- Dynamic programming, DFS
- Longest increasing subsequence
- mono deque
Minimum/Shortest
- Dynammic programming, DFS
- BFS
Partition/split ... array/string
Subsequence
- Dynamic programming, DFS
- Longest increasing subsequence
- Sliding window
- Longest increasing subsequence
Matrix
- BFS, DFS
- Dyanmic programming
Jump
Game
Connected component, Cut/remove, Regions/groups/connections
- Union Find
- Number of connected components, Redundant connections
Transitive relationship
- If the items are related to one another and the relationship is transitive
- then chances are we can build a graph and use BFS or Union Find
- string converting to another, BFS
- string converting to another, BFS, Union Find
- numbers having divisional relationship, BFS, Union Find
Interval
- Greedy
- sort by start/end time and then go through sorted intervals Interval Pattern