1. Algorithm Analysis
1.1 Steps
- Find the state and choice
for(auto state1: all possible value) {
for(auto state2: all possible value) {
for ...
dp[state1][state2][...] = min/max(choice1, choice2...);
}
}
- Define the dp table
- Dimension of dp table is the same with the number of states, if not do the optimization
- Initialize the base case
- Clear the results index in dp table, usually first or last of the table
- State transition
- dp[state1][state2][...] = min/max(choice1, choice2...);
- handle the overflow cases
- State compression
- check the state transition to optimize the space cost of dp table
1.2 Templates
1.2.1 Knapsack problem
int knapsack(int w, vector<int> &wt, vector<int> &val) {
int n = wt.size();
//create dp table
vector<vector<int>> dp(n+1, vector<int>(w, 0));
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= w; j++) {
if(j < wt[i-1]) {
dp[i][j] = dp[i-1][j];
} else {
dp[i][j] = max(dp[i-1][j], dp[i-1][j-wt[i-1]]+val[i-1]);
}
}
}
return dp[n][w];
}
1.2.2 longest common subsequence
int n1 = s1.size(), n2 = s2.size();
//s1[0..i-1] and s2[0..j-1] lcs is dp[i][j]
vector<vector<int>> dp(n1+1, vector<int>(n2+1,0));
for(int i = 1; i <= n1; i++) {
for(int j = 1; j <= n2; j++) {
if(s1[i-1] == s2[j-1]) { //choice 1
dp[i][j] = dp[i-1][j-1] + 1;
} else { // choice 2 and 3
dp[i][j] = max(dp[i-1][j], // s2[j] not in lcs
dp[i][j-1]); // s1[i] not in lcs
}
}
}
return dp[n1][n2];
1.2.3 Longest Increasing Subsequence
int LIS(vector<int> &nums){
int n = nums.size();
int ans = 0;
//dp[i] is the LIS of nums[0...i]
vector<int> dp(n);
//base case
for(auto &it:do) {
it = 1;
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < i; j++) {
if(nums[j] < nums[i]) {
dp[i] = max(dp[i], dp[j]+1);
}
}
ans = max(ans, dp[i]);
}
return ans;
}