[Leetcode] Dynamic Programming

 

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值