This is a easy way to learn Dynamic Programming
DP is known as bottom-up and top-down.
Top-down is like a recursion, but with memoized, when using top-down, draw tree.
Bottom-up is like careful brute force, you need to track all the subquestions’ answer for further use.
This is a picture from MIT course that explain the fib questions use bottom-up, I actually like bottom-up way, because it is easy to understand, you need to draw a table to find the optimal solution, although you can use it for fib one.
Here are some dp classic questions.
1. *Find a Path***
Leetcode 62. Unique Paths
A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).
How many possible unique paths are there?
1 | class Solution { |
63.*Unique Paths II***
robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).
Now consider if some obstacles are added to the grids. How many unique paths would there be?
1 | class Solution { |