Big O
Notes from Cracking the Coding Interview - 6th Edition
Time & Space Complexity
Drop the Non-Dominant Terms
becomes
becomes
becomes
Multi-Part Algorithms: Add vs Multiply
ADD
MULTIPLY
Amortized Time
adds takes time. The amortized time for each adding is .
Log N Runtimes
binary search example:
With each comparison, we go either left or right. Half the nodes are on each side, so we cut the problem space in half each time.
Recursive Runtime
Remember pattern: when you have a recursive function that makes multiple calls, the runtime will ofter look like , where branches is the number of times each recursive call branches. In this case, this gives us .
N | Level | #Nodes | Also expressed as... | Or... |
4 | 0 | 1 | ||
3 | 1 | 2 | 2 * previous level = 2 | |
2 | 2 | 4 | 2 * previous level = | |
1 | 3 | 8 | 2 * previous level = |
Sorting string takes
Examples
Important to note here is line 3 in the code: int j = i + 1
(see page 46/47)
The most inner for loop is considered as a constant -> 100,000 units.
Balanced binary search tree (see page 49).
Last updated