Problem solution · C++

Minimum Increments for Target Multiples in an Array

Minimum Increments for Target Multiples in an Array: a C++ solution using dynamic programming. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Dynamic programming
Source
walkccc 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 Minimum Increments for Target Multiples in an Array, 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 3444.cpp.
  • The implementation visibly relies on sequence storage, hash lookup, cached states.
  • 7 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 walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeMinimum Increments for Target Multiples in an Array · C++C++
Use this to learn the idea, then write your own version.
class Solution { public:  int minimumIncrements(vector<int>& nums, vector<int>& target) {    const int maxMask = 1 << target.size();    unordered_map<int, long> maskToLcm;     for (int mask = 1; mask < maxMask; ++mask) {      const vector<int> subset = getSubset(mask, target);      maskToLcm[mask] = getLcm(subset);    }     // dp[mask] := the minimum number of increments to make each number in the    // subset of target have at least one number that is a multiple in `num`,    // where `mask` is the bitmask of the subset of target    vector<long> dp(maxMask, LONG_MAX);    dp[0] = 0;     for (const int num : nums) {      // maskToCost := (mask, cost), where `mask` is the bitmask of the subset      // of target and `cost` is the minimum number of increments to make each      // number in the subset of target have at least one number that is a      // multiple in `num`      vector<pair<int, long>> maskToCost;      for (const auto& [mask, lcm] : maskToLcm) {        const int remainder = num % lcm;        maskToCost.emplace_back(mask, remainder == 0 ? 0 : lcm - remainder);      }      vector<long> newDp = dp;      for (int prevMask = 0; prevMask < maxMask; ++prevMask) {        if (dp[prevMask] == LONG_MAX)          continue;        for (const auto& [mask, cost] : maskToCost) {          const int newMask = prevMask | mask;          newDp[newMask] = min(newDp[newMask], dp[prevMask] + cost);        }      }      dp = std::move(newDp);    }     return dp.back() == LONG_MAX ? -1 : dp.back();  }  private:  vector<int> getSubset(int mask, const vector<int>& target) {    vector<int> subset;    for (int i = 0; i < target.size(); ++i)      if (mask >> i & 1)        subset.push_back(target[i]);    return subset;  }   long getLcm(const vector<int>& nums) {    long res = 1;    for (const int num : nums)      res = lcm(res, num);    return res;  }}; 

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 ↗