0-1背包二维数组模板代码

详细解析🔗

  1. public static void main(String[] args) {
  2. int[] weight = {1, 3, 4};
  3. int[] value = {15, 20, 30};
  4. int bagsize = 4;
  5. testweightbagproblem(weight, value, bagsize);
  6. }
  7. public static void testweightbagproblem(int[] weight, int[] value, int bagsize) {
  8. // 定义dp数组:dp[i][j]表示背包容量为j时,前i个物品能获得的最大价值
  9. int[][] dp = new int[weight.length][bagsize + 1];
  10. // 初始化:背包容量为0时,能获得的价值都为0
  11. for (int i = weight[0]; i <= bagsize; i++) {
  12. dp[0][i] = value[0];
  13. }
  14. // 遍历顺序:先遍历物品,再遍历背包容量(可以交换)
  15. for (int i = 1; i < weight.length; i++) { // 遍历物品
  16. for (int j = 0; j <= bagsize; j++) { // 遍历背包
  17. if (j < weight[i]) {
  18. dp[i][j] = dp[i - 1][j];
  19. } else {
  20. dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]);
  21. }
  22. }
  23. }
  24. //打印dp数组
  25. for (int i = 0; i < weight.length; i++) {
  26. for (int j = 0; j <= bagsize; j++) {
  27. System.out.print(dp[i][j] + " ");
  28. }
  29. System.out.print("\n");
  30. }
  31. }

image.png

滚动数组的优化版本

注意:一维数组遍历背包只能倒序遍历背包,防止物品多次放入。
倒序就是先算dp[2]
dp[2] = dp[2 - weight[0]] + value[0] = 15 (dp数组已经都初始化为0)
dp[1] = dp[1 - weight[0]] + value[0] = 15
且应该先遍历物品,在遍历背包,因为在二维数组中,通过dp[i][j-1]是由上一层推出,并且不会被覆盖,而一维数组如果从前向后遍历,那么就会被覆盖。

  1. public static void main(String[] args) {
  2. int[] weight = {1, 3, 4};
  3. int[] value = {15, 20, 30};
  4. int bagsize = 4;
  5. testweightbagproblem(weight, value, bagsize);
  6. }
  7. public static void testweightbagproblem(int[] weight, int[] value, int bagsize) {
  8. int[] dp = new int[bagsize + 1];
  9. // 遍历物品
  10. for (int i = 0; i < weight.length; i++) {
  11. // 遍历背包
  12. for (int j = bagsize; j >= weight[i]; j--) {
  13. dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]);
  14. }
  15. }
  16. for (int i = 0; i < bagsize; i++) {
  17. System.out.print(dp[i] + " ");
  18. }
  19. }

image.png