Problem solution · C++

Maximum Number of Items from Sale I

Maximum Number of Items from Sale I: 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
96 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 Number of Items from Sale I, 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

  • 96 lines of C++ from the credited upstream file maximum-number-of-items-from-sale-i.cpp.
  • The implementation visibly relies on sequence storage, cached states.
  • 15 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 Number of Items from Sale I · C++C++
Use this to learn the idea, then write your own version.
// Time:  O(rlogr + n * b), r = max(f for f, _ in items)// Space: O(r + b) // freq table, knapsack dp, greedyclass Solution {public:    int maximumSaleItems(vector<vector<int>>& items, int budget) {        static const int NEG_INF = numeric_limits<int>::min();         int max_f = 0;        for (const auto& x : items) {            max_f = max(max_f, x[0]);        }        vector<int> cnt(max_f + 1);        for (const auto& x : items) {            ++cnt[x[0]];        }        vector<int> total(size(cnt));        for (int i = 1; i < size(cnt); ++i) {            if (!cnt[i]) {                continue;            }            for (int j = i; j < size(cnt); j += i) {                total[i] += cnt[j];            }        }        vector<int> dp(budget + 1, NEG_INF);        dp[0] = 0;        for (const auto& x : items) {            for (int i = size(dp) - 1; i - x[1] >= 0; --i) {                if (dp[i - x[1]] == NEG_INF) {                    continue;                }                dp[i] = max(dp[i], dp[i - x[1]] + total[x[0]]);            }        }        int min_p = numeric_limits<int>::max();        for (const auto& x : items) {            min_p = min(min_p, x[1]);        }        int result = 0;        for (int i = 0; i < size(dp); ++i) {            if (dp[i] == NEG_INF) {                continue;            }            result = max(result, dp[i] + (budget - i) / min_p);        }        return result;    }}; // Time:  O(rlogr + n * b), r = max(f for f, _ in items)// Space: O(r + b)// freq table, knapsack dp, greedyclass Solution2 {public:    int maximumSaleItems(vector<vector<int>>& items, int budget) {        static const int NEG_INF = numeric_limits<int>::min();         int max_f = 0;        for (const auto& x : items) {            max_f = max(max_f, x[0]);        }        vector<int> cnt(max_f + 1);        for (const auto& x : items) {            ++cnt[x[0]];        }        vector<int> total(size(cnt));        for (int i = 1; i < size(cnt); ++i) {            if (!cnt[i]) {                continue;            }            for (int j = i; j < size(cnt); j += i) {                total[i] += cnt[j];            }        }        vector<int> dp(budget + 1, NEG_INF);        dp[0] = 0;        for (const auto& x : items) {            for (int i = size(dp) - 1; i - x[1] >= 0; --i) {                if (dp[i - x[1]] == NEG_INF) {                    continue;                }                dp[i] = max(dp[i], dp[i - x[1]] + total[x[0]]);            }            for (int i = x[1]; i < size(dp); ++i) {                if (dp[i - x[1]] == NEG_INF) {                    continue;                }                dp[i] = max(dp[i], dp[i - x[1]] + 1);            }        }        return ranges::max(dp);    }}; 

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 ↗