跳至主要內容

01背包最大盈利


01背包最大盈利

题目

已知N个物品的重量和利润,我们将这些物品放入容量为C的背包。目标是背包中的物品中获得最大的利润。每个物品只能选择一次。

示例:

物品:物品1、物品2、物品3、物品4
单个物品重量:2、3、1、4
单个物品盈利:4、5、3、7
背包容量:5

解法

暴力解法

class Knapsack {
    public int solveKnapsack(int[] profits, int[] weights, int capacity) {
        return this.knapsackRecursive(profits, weights, capacity, 0);
    }
	//currentIndex是用于确定当前遍历到的元素的索引
    private int knapsackRecursive(int[] profits, int[] weights, int capacity, int currentIndex) {
        //基准检查
        if (capacity <= 0 || currentIndex >= profits.length)
            return 0;

        //选择当前物品后递归调用
        //如果当前物品的重量超过容量,则不继续递归调用
        int profit1 = 0;
        if( weights[currentIndex] <= capacity )
            profit1 = profits[currentIndex] + knapsackRecursive(profits, weights,
                                                                capacity - weights[currentIndex], currentIndex + 1);

        //排除当前索引的物品后递归调用
        int profit2 = knapsackRecursive(profits, weights, capacity, currentIndex + 1);

        return Math.max(profit1, profit2);
    }

    public static void main(String[] args) {
        Knapsack ks = new Knapsack();
        int[] profits = {1, 6, 10, 16};
        int[] weights = {1, 2, 3, 5};
        int maxProfit = ks.solveKnapsack(profits, weights, 7);
        System.out.println("Total knapsack profit ---> " + maxProfit);
        maxProfit = ks.solveKnapsack(profits, weights, 6);
        System.out.println("Total knapsack profit ---> " + maxProfit);
  }
}

自顶向下(备忘录)解法

class Knapsack {

  public int solveKnapsack(int[] profits, int[] weights, int capacity) {
    Integer[][] dp = new Integer[profits.length][capacity + 1];
    return this.knapsackRecursive(dp, profits, weights, capacity, 0);
  }

  private int knapsackRecursive(Integer[][] dp, int[] profits, int[] weights, int capacity,
      int currentIndex) {

    //基础检查
    if (capacity <= 0 || currentIndex >= profits.length)
      return 0;

    // 如果我们已经解决过相似的问题,那么直接从dp二维表中返回相似的结果
    if(dp[currentIndex][capacity] != null)
      return dp[currentIndex][capacity];

    //选择当前物品后递归调用
    //如果当前物品的重量超过容量,则不继续递归调用
    int profit1 = 0;
    if( weights[currentIndex] <= capacity )
        profit1 = profits[currentIndex] + knapsackRecursive(dp, profits, weights,
                capacity - weights[currentIndex], currentIndex + 1);

    // 排除当前索引指向的物品后递归调用
    int profit2 = knapsackRecursive(dp, profits, weights, capacity, currentIndex + 1);
      
    //返回结果之前,先将结果存到dp二维表中
    dp[currentIndex][capacity] = Math.max(profit1, profit2);
    return dp[currentIndex][capacity];
  }

  public static void main(String[] args) {
    Knapsack ks = new Knapsack();
    int[] profits = {1, 6, 10, 16};
    int[] weights = {1, 2, 3, 5};
    int maxProfit = ks.solveKnapsack(profits, weights, 7);
    System.out.println("Total knapsack profit ---> " + maxProfit);
    maxProfit = ks.solveKnapsack(profits, weights, 6);
    System.out.println("Total knapsack profit ---> " + maxProfit);
  }
}

自底向上

class Knapsack {

  public int solveKnapsack(int[] profits, int[] weights, int capacity) {
    // 基准检查
    if (capacity <= 0 || profits.length == 0 || weights.length != profits.length)
      return 0;

    int n = profits.length;
    int[][] dp = new int[n][capacity + 1];

    // 填充capacity=0的那列,0容量0利润
    for(int i=0; i < n; i++)
      dp[i][0] = 0;

    // 如果我们只有一个重量,如果它不超过承载量,我们就接受它
    for(int c=0; c <= capacity; c++) {
      if(weights[0] <= c)
        dp[0][c] = profits[0];
    }

    // 为所有容量处理所有子数组
    for(int i=1; i < n; i++) {
      for(int c=1; c <= capacity; c++) {
        int profit1= 0, profit2 = 0;
        // 如果当前元素的重量不超过容量,包含当前元素
        if(weights[i] <= c)
          profit1 = profits[i] + dp[i-1][c-weights[i]];
        // 不包含当前项
        profit2 = dp[i-1][c];
        // 取profit1和profit2中的最大值
        dp[i][c] = Math.max(profit1, profit2);
      }
    }

    //右下角为最大的利润
    return dp[n-1][capacity];
  }

  public static void main(String[] args) {
    Knapsack ks = new Knapsack();
    int[] profits = {1, 6, 10, 16};
    int[] weights = {1, 2, 3, 5};
    int maxProfit = ks.solveKnapsack(profits, weights, 7);
    System.out.println("Total knapsack profit ---> " + maxProfit);
    maxProfit = ks.solveKnapsack(profits, weights, 6);
    System.out.println("Total knapsack profit ---> " + maxProfit);
  }
}
上次编辑于:
贡献者: Neil