Problem solution · C++

Maximum Weight in Two Bags

Maximum Weight in Two Bags: a C++ solution using dynamic programming. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Dynamic programming
Source
Kamyu LeetCode Solutions
Length
59 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Dynamic programming

For Maximum Weight in Two Bags, the implementation records answers for smaller states and reuses them to build the requested result without repeating work.

  1. Define precisely what one DP state represents.
  2. Establish the base cases before transitions are evaluated.
  3. Process states in dependency order and combine only already-known values.

Code notes

  • 59 lines of C++ from the credited upstream file maximum-weight-in-two-bags.cpp.
  • The implementation visibly relies on sequence storage, cached states.
  • 9 loop blocks detected.

Complexity

Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeMaximum Weight in Two Bags · C++C++
Use this to learn the idea, then write your own version.
// Time:  O(n * w1 * w2)// Space: O(w1 * w2) // dp, bitsetclass Solution {public:    int maxWeight(vector<int>& weights, int w1, int w2) {        static const int MAX_W = 300;         vector<bitset<MAX_W + 1>> dp(w1 + 1), new_dp(w1 + 1);        dp[0][0] = 1;        for (const auto& w : weights) {            for (int i = 0; i <= w1; ++i) {                new_dp[i] = dp[i] | (i - w >= 0 ? dp[i - w] : 0) | (dp[i] << w);            }            swap(dp, new_dp);        }        int result = 0;        for (int i = 0; i <= w1; ++i) {            for (int j = w2; j >= 0; --j) {                if (dp[i][j]) {                    result = max(result, i + j);                    break;                }            }        }        return result;    }}; // Time:  O(n * w1 * w2)// Space: O(w1 * w2)// dpclass Solution2 {public:    int maxWeight(vector<int>& weights, int w1, int w2) {        vector<vector<bool>> dp(w1 + 1, vector<bool>(w2 + 1)), new_dp(w1 + 1, vector<bool>(w2 + 1));        dp[0][0] = true;        for (const auto& w : weights) {            for (int i = 0; i <= w1; ++i) {                for (int j = 0; j <= w2; ++j) {                    new_dp[i][j] = dp[i][j] || (i - w >= 0 && dp[i - w][j]) || (j - w >= 0 && dp[i][j - w]);                }            }            swap(dp, new_dp);        }        int result = 0;        for (int i = 0; i <= w1; ++i) {            for (int j = w2; j >= 0; --j) {                if (dp[i][j]) {                    result = max(result, i + j);                    break;                }            }        }        return result;    }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗