动态规划之二:0/1背包问题

一、0/1背包问题定义

有一个背包,它的容量为C(Capacity)。现在有n种不同的物品,编号为0...n-1,其中每一件物品的重量为w(i),价值为v(i)。问可以向这个背包中盛放哪些物品,使得在不超过背包容量的基础上,物品的总价值最大。

F(n, C):考虑将n个物品放进容量为C的背包,使得价值最大。

状态转移方程: F(i, c) = max ( F(i-1, c), v(i) + F(i-1, c-w(i) ) ) 

基本代码:

#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
using namespace std;

class Solution {
public:
    int knapsack01(const vector<int>& w, const vector<int>& v, int c) {
        int n = w.size();
        if(n == 0) return 0;
        //memo[i][j] 为在容量为j时候,放第i个物品时候的最大价值
        vector<vector<int> > memo(n, vector<int>(c+1, 0));
        for(int j = 0; j <= c; j++){
            memo[0][j] = 0;
            if(j >= w[0]) memo[0][j] = v[0];
        }
        
        for(int i = 1; i < n; i++){
            for(int j = 1; j <= c; j++){
                memo[i][j] = memo[i-1][j];
                if(j >= w[i]) memo[i][j] = max(memo[i-1][j], v[i] + memo[i-1][j-w[i]]);
            }
        }
        
        return memo[n-1][c];
    }
};

int main(int argc, char **argv)
{
    Solution solution;
    vector<int> w;
    w.push_back(1);
    w.push_back(2);
    w.push_back(3);
    vector<int> v;
    v.push_back(6);
    v.push_back(10);
    v.push_back(12);
    int res = solution.knapsack01(w, v, 5);
    printf("%d\n",res);
    return 0;
}

使用两行数组:

class Solution {
public:
    int knapsack01(const vector<int>& w, const vector<int>& v, int c) {
        int n = w.size();
        if(n == 0) return 0;
        //memo[j] 为在容量为j时候,放第i个物品时候的最大价值
        vector<vector<int> > memo(2, vector<int>(c+1,0));
        
        for(int j = 0; j <= c; j++){
            memo[0][j] = 0;
            if(j >= w[0]) memo[0][j] = v[0];
        }

        for(int i = 1; i < n; i++){
            for(int j = 1; j <= c; j++){
                memo[i%2][j] = memo[(i-1)%2][j];
                if(j >= w[i]) {
                    memo[i%2][j] = max(memo[(i-1)%2][j], v[i] + memo[(i-1)%2][j-w[i]]);
                }
                printf("%d-%d-%d\n",i, j, memo[i%2][j]);
            }
        }
        printf("%d\n",memo[(n-1)%2][c]);
        return memo[(n-1)%2][c];
    }
};

使用一维数组:

class Solution {
public:
    int knapsack01(const vector<int>& w, const vector<int>& v, int c) {
        int n = w.size();
        if(n == 0) return 0;
        //memo[j] 为在容量为j时候,放第i个物品时候的最大价值
        vector<int > memo(c+1,0);
        
        for(int j = 0; j <= c; j++){
            memo[j] = 0;
            if(j >= w[0]) memo[j] = v[0];
        }

        for(int i = 1; i < n; i++){
            for(int j = c; j >= w[i]; j--){
                if(j >= w[i]) {
                    memo[j] = max(memo[j], v[i] + memo[j-w[i]]);
                }
                printf("%d-%d-%d\n",i, j, memo[j]);
            }
        }
        printf("%d\n",memo[c]);
        return memo[c];
    }
};

二、leetcode问题举例

416Partition Equal Subset Sum

Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.

Note:

  1. Each of the array element will not exceed 100.
  2. The array size will not exceed 200.

Example 1:

Input: [1, 5, 11, 5]

Output: true

Explanation: The array can be partitioned as [1, 5, 5] and [11].

Example 2:

Input: [1, 2, 3, 5]

Output: false

Explanation: The array cannot be partitioned into equal sum subsets.
class Solution {
public:
    bool canPartition(vector<int>& nums) {
        int n = nums.size();
        int sum = 0;
        for(int i = 0; i < n; i++){
            sum += nums[i];
        }
        if(sum % 2 != 0) return false;
        int c = sum / 2;
        vector<bool> memo(c+1, false);
        for(int j = 1; j <= c; j++){
            if(nums[0] == j) memo[j] = true;
        }
        for(int i = 1; i < n; i++){
            for(int j = c; j >= nums[i]; j--){
                memo[j] = memo[j-nums[i]] || memo[j];
            }
        }
        return memo[c];
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值