Backpack II

Question

Problem Statement

Given n items with size Ai and value Vi, and a backpack with size m.
What’s the maximum value can you put into the backpack?

Example

Given 4 items with size [2, 3, 5, 7] and value [1, 5, 2, 4], and a
backpack with size 10. The maximum value is 9.

Note

You cannot divide item into small pieces and the total size of items you
choose should smaller or equal to m.

Challenge

O(n x m) memory is acceptable, can you do it in O(m) memory?

题解

首先定义状态 K(i,w) 为前 i 个物品放入size为 w 的背包中所获得的最大价值,则相应的状态转移方程为:
K(i,w) = \max {K(i-1, w), K(i-1, w - w_i) + v_i}

详细分析过程见 Knapsack

C++ - 2D vector for result

  1. class Solution {
  2. public:
  3. /**
  4. * @param m: An integer m denotes the size of a backpack
  5. * @param A & V: Given n items with size A[i] and value V[i]
  6. * @return: The maximum value
  7. */
  8. int backPackII(int m, vector<int> A, vector<int> V) {
  9. if (A.empty() || V.empty() || m < 1) {
  10. return 0;
  11. }
  12. const int N = A.size() + 1;
  13. const int M = m + 1;
  14. vector<vector<int> > result;
  15. result.resize(N);
  16. for (vector<int>::size_type i = 0; i != N; ++i) {
  17. result[i].resize(M);
  18. std::fill(result[i].begin(), result[i].end(), 0);
  19. }
  20. for (vector<int>::size_type i = 1; i != N; ++i) {
  21. for (int j = 0; j != M; ++j) {
  22. if (j < A[i - 1]) {
  23. result[i][j] = result[i - 1][j];
  24. } else {
  25. int temp = result[i - 1][j - A[i - 1]] + V[i - 1];
  26. result[i][j] = max(temp, result[i - 1][j]);
  27. }
  28. }
  29. }
  30. return result[N - 1][M - 1];
  31. }
  32. };

Java

  1. public class Solution {
  2. /**
  3. * @param m: An integer m denotes the size of a backpack
  4. * @param A & V: Given n items with size A[i] and value V[i]
  5. * @return: The maximum value
  6. */
  7. public int backPackII(int m, int[] A, int V[]) {
  8. if (A == null || V == null || A.length == 0 || V.length == 0) return 0;
  9. final int N = A.length;
  10. final int M = m;
  11. int[][] bp = new int[N + 1][M + 1];
  12. for (int i = 0; i < N; i++) {
  13. for (int j = 0; j <= M; j++) {
  14. if (A[i] > j) {
  15. bp[i + 1][j] = bp[i][j];
  16. } else {
  17. bp[i + 1][j] = Math.max(bp[i][j], bp[i][j - A[i]] + V[i]);
  18. }
  19. }
  20. }
  21. return bp[N][M];
  22. }
  23. }

源码分析

  1. 使用二维矩阵保存结果result
  2. 返回result矩阵的右下角元素——背包size限制为m时的最大价值

按照第一题backpack的思路,这里可以使用一维数组进行空间复杂度优化。优化方法为逆序求result[j],优化后的代码如下:

C++ 1D vector for result

  1. class Solution {
  2. public:
  3. /**
  4. * @param m: An integer m denotes the size of a backpack
  5. * @param A & V: Given n items with size A[i] and value V[i]
  6. * @return: The maximum value
  7. */
  8. int backPackII(int m, vector<int> A, vector<int> V) {
  9. if (A.empty() || V.empty() || m < 1) {
  10. return 0;
  11. }
  12. const int M = m + 1;
  13. vector<int> result;
  14. result.resize(M);
  15. std::fill(result.begin(), result.end(), 0);
  16. for (vector<int>::size_type i = 0; i != A.size(); ++i) {
  17. for (int j = m; j >= 0; --j) {
  18. if (j < A[i]) {
  19. // result[j] = result[j];
  20. } else {
  21. int temp = result[j - A[i]] + V[i];
  22. result[j] = max(temp, result[j]);
  23. }
  24. }
  25. }
  26. return result[M - 1];
  27. }
  28. };

Reference