
The Fundamental Of Coding Interview Question part 3 (Javascript)
Posted on
Recusrion and Dynamic Programming(DP)
Ways to approach:
- Bottom-up: Starting from the simplest case, then build a solution that on top of the previous one.
- Top-Down: The reverse of Bottom-up, less intuitive, but sometimes useful.
- Half-Half: They are usually seen on the bitwise operation and binary tree operation.
DP & memoization:
- Using recursion alone sometimes is not efficient enough, need to add Memoization to avoid doing repeat calculation.
- For recursion to work, a return on the base case is needed, so is what should be returned, is it the recursive function its self, the counter, or the result array?
- Find the pattern of recursion is the most important one.
- using an algorithm in a recursive way takes a lot of space, but doing the same algorithm in an iterative way is not easy sometimes, it's all about the trade-off.
- when naming variables on the matrix, put y at first, x at second, like this: matrix[y][x]