Problem solution · C++

Divide an Array Into Subarrays With Minimum Cost II

Divide an Array Into Subarrays With Minimum Cost II: a C++ solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Direct simulation
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

Direct simulation

For Divide an Array Into Subarrays With Minimum Cost II, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 59 lines of C++ from the credited upstream file 3013.cpp.
  • The implementation visibly relies on sequence storage.
  • 4 loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

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 codeDivide an Array Into Subarrays With Minimum Cost II · C++C++
Use this to learn the idea, then write your own version.
class Solution { public:  long long minimumCost(vector<int>& nums, int k, int dist) {    // Equivalently, the problem is to find nums[0] + the minimum sum of the top    // k - 1 numbers in nums[i..i + dist], where i > 0 and i + dist < n.    long windowSum = 0;    multiset<int> selected;    multiset<int> candidates;     for (int i = 1; i <= dist + 1; ++i) {      windowSum += nums[i];      selected.insert(nums[i]);    }     windowSum = balance(windowSum, selected, candidates, k);    long minWindowSum = windowSum;     for (int i = dist + 2; i < nums.size(); ++i) {      const int outOfScope = nums[i - dist - 1];      if (selected.find(outOfScope) != selected.end()) {        windowSum -= outOfScope;        selected.erase(selected.find(outOfScope));      } else {        candidates.erase(candidates.find(outOfScope));      }      if (nums[i] < *selected.rbegin()) {  // nums[i] is a better number.        windowSum += nums[i];        selected.insert(nums[i]);      } else {        candidates.insert(nums[i]);      }      windowSum = balance(windowSum, selected, candidates, k);      minWindowSum = min(minWindowSum, windowSum);    }     return nums[0] + minWindowSum;  }  private:  // Returns the updated `windowSum` by balancing the multiset `selected` to  // keep the top k - 1 numbers.  long balance(long windowSum, multiset<int>& selected,               multiset<int>& candidates, int k) {    while (selected.size() < k - 1) {      const int minCandidate = *candidates.begin();      windowSum += minCandidate;      selected.insert(minCandidate);      candidates.erase(candidates.find(minCandidate));    }    while (selected.size() > k - 1) {      const int maxSelected = *selected.rbegin();      windowSum -= maxSelected;      selected.erase(selected.find(maxSelected));      candidates.insert(maxSelected);    }    return windowSum;  }}; 

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 ↗