美文网首页
0/1背包和多重背包问题

0/1背包和多重背包问题

作者: MrWheat | 来源:发表于2019-07-30 14:43 被阅读0次

Given weights and values of n items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack. In other words, given two integer arrays val[0..n-1] and wt[0..n-1] which represent values and weights associated with n items respectively. Also given an integer W which represents knapsack capacity, find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to W. You cannot break an item, either pick the complete item, or don’t pick it (0-1 property).


Screen Shot 2019-07-29 at 11.35.57 PM.png
// A Dynamic Programming based solution for 0-1 Knapsack problem 
class Knapsack 
{ 
   // Returns the maximum value that can be put in a knapsack of capacity W 
    static int knapSack(int W, int wt[], int val[], int n) 
    { 
         int i, w; 
     int K[][] = new int[n+1][W+1]; 
       
     // Build table K[][] in bottom up manner 
     for (i = 0; i <= n; i++) 
     { 
         for (w = 0; w <= W; w++) 
         { 
             if (i==0 || w==0) 
                  K[i][w] = 0; 
             else if (wt[i-1] <= w) 
                   K[i][w] = Math.max(val[i-1] + K[i-1][w-wt[i-1]],  K[i-1][w]); 
             else
                   K[i][w] = K[i-1][w]; 
         } 
      } 
       
      return K[n][W]; 
    } 
}

0/1背包问题,也可以被应用到求数组sum的问题,https://leetcode.com/problems/partition-equal-subset-sum/discuss/90592/01-knapsack-detailed-explanation
特点:选几个元素,拿到最大,能不能达到之类的。

多重背包问题

有一系列按钮,每个按钮按下去会得到一定体积范围的可乐。先给定一个目标体积范围,问不限制按按钮次数,能否确定一定能得到目标范围内的可乐?
举例:有三个按钮,按下去得到的范围是[100, 120], [200, 240], [400, 410],
假设目标是[100, 110], 那答案是不能。因为按下一,可能得到120体积的可乐,不在目标范围里。
假设目标是[90, 120],那答案是可以。因为按下一,一定可以得到此范围内的可乐。
假设目标是[300, 360], 那答案是可以,因为按下一再按二,一定可以得到此范围内
假设目标是[310, 360], 那答案是不能,因为按下一再按二,有可能得到300,永远没可能确定得到这个范围内的可乐。
假设目标是[1, 9999999999],那答案是可以。随便按一个都确定满足此范围。

public static boolean coke(List<List<Integer>> buttons, List<Integer> target) {
    int m = target.get(0);
    int n = target.get(1);
    boolean[][] dp = new boolean[m + 1][n + 1];
    
    //Init
    for (int i = 0; i <= m; ++i) {
      for (int j = 0; j <= n; ++j) {
        for (List<Integer> button : buttons) {
          if (i <= button.get(0) && j >= button.get(1)) {
            dp[i][j] = true;
            break;
          }
        }
      }
    }
  
    for (int i = 0; i <= m; ++i) {
      for (int j = 0; j <= n; ++j) {
        for (List<Integer> button : buttons) {
          int preL = i - button.get(0);
          int preR = j - button.get(1);
          if (preL >= 0 && preR >= 0 && dp[preL][preR]) {
            dp[i][j] = true;
           break;
          }
        }
      }
    }
    
    return dp[m][n];
  }

多重背包问题:每个可以拿多次,求上下节或者范围之类的,从拿到的数值入手。

相关文章

  • 背包问题

    背包问题属于典型的动态规划问题。这里我们将详细介绍0-1背包,完全背包和多重背包问题 一、 0-1背包 有N件物品...

  • 0/1背包和多重背包问题

    Given weights and values of n items, put these items in a...

  • 算法-动态规划-背包问题

    背包问题是基础的动态规划问题,包含了0-1背包,完全背包,多重背包等。 0-1背包 存在容量为 的背包 , 件体...

  • lintcode-k数和

    动态规划(确定0-1背包、完全背包、多重背包)0-1背包:每个元素要么出现,要么不出现,逆序遍历,数组定义为:前i...

  • Algorithm进阶计划 -- 动态规划(下)

    经典动态规划背包问题最长子序列问题 1. 背包问题 1.1 0-1 背包问题 0-1 背包问题,描述如下: 上面...

  • Chapter10——动态规划——背包问题

    1. 题目列表 POJ1837(变形的0-1背包问题,好好理解题意) POJ1276(多重背包问题、二进制优化)结...

  • 01-背包、完全背包、多重背包及其相关应用

    本文介绍了背包问题系列,主要包括: 【1】 01-背包及其应用【2】完全背包及其应用【3】多重背包 【1】01-背...

  • 背包问题3(多重背包)

    上一篇讲的完全背包是指在所有物品件数无限多的情况下选择最值,现在引申出多重背包问题,即各物品个数w[ i ]均有限...

  • 一刷到底。。

    归并快排堆排序模拟堆01背包完全背包问题多重背包问题多重背包问题2链表排序多链表合并字符串哈希字典树单调栈单调队列...

  • 背包

    多重背包多重背包模板

网友评论

      本文标题:0/1背包和多重背包问题

      本文链接:https://www.haomeiwen.com/subject/wthnrctx.html