Can depend on:
- data structures that grow in some proportion to input
- a call stack that grows in some proportion to input
Rather than comparing each element to every other element, keep track of relevant max/mins as you go
- Benefits:
- Saves time compared to nested loops
- Used in:
- Best Time to Buy and Sell Stock
Can be for loop with two variables:
for (let i=0, j=arr.length-1; i <= j; i++, j--){
// ...
}
Or while loop with left/right:
let left = 0
let right = 0
while (left < right) {
// ...
left ++
right --
}
- Benefits:
- Saves space compared to creating a new array to track values
- Used in:
- Valid Palindromes
Can use regex, .replace() (or equivalent)
- Benefits:
- Saves time and space compared to creating a new entity
- Used in:
- Valid Palindromes
Works well for fractal things (e.g. trees) โ that is, things whose zoomed-in pieces look similar to the zoomed-out whole
- Used in:
- Invert Binary Tree
Uses recursion to reach the lowest node of one branch of a tree, then progresses to subsequent branches
- Used in:
- Invert Binary Tree