Problem solution · C++

Minimum Operations to Make Elements Within K Subarrays Equal

Minimum Operations to Make Elements Within K Subarrays Equal: 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
85 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 Minimum Operations to Make Elements Within K Subarrays Equal, 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

  • 85 lines of C++ from the credited upstream file 3505.cpp.
  • The implementation visibly relies on sequence storage.
  • 1 loop block 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 codeMinimum Operations to Make Elements Within K Subarrays Equal · C++C++
Use this to learn the idea, then write your own version.
class Solution { public:  long long minOperations(vector<int>& nums, int x, int k) {    // minOps[i] := the minimum number of operations needed to make    // nums[i..i + x - 1] equal to the median    const vector<long> minOps = getMinOps(nums, x);    vector<vector<long>> mem(nums.size() + 1, vector<long>(k + 1, -1));    return minOperations(nums, x, 0, k, minOps, mem);  }  private:  static constexpr long kInf = LONG_MAX / 2;   // Returns the minimum operations needed to have at least k non-overlapping  // subarrays of size x in nums[i..n - 1].  long minOperations(const vector<int>& nums, int x, int i, int k,                     const vector<long>& minOps, vector<vector<long>>& mem) {    if (k == 0)      return 0;    if (i == nums.size())      return kInf;    if (mem[i][k] != -1)      return mem[i][k];    const long skip = minOperations(nums, x, i + 1, k, minOps, mem);    const long pick =        i + x <= nums.size()            ? minOps[i] + minOperations(nums, x, i + x, k - 1, minOps, mem)            : kInf;    return mem[i][k] = min(skip, pick);  }   // Returns the minimum operations needed to make all elements in the window of  // size x equal to the median.  vector<long> getMinOps(const vector<int>& nums, int x) {    vector<long> minOps;    multiset<int> lower;    multiset<int> upper;    long lowerSum = 0;    long upperSum = 0;    for (int i = 0; i < nums.size(); ++i) {      if (lower.empty() || nums[i] <= *lower.rbegin()) {        lower.insert(nums[i]);        lowerSum += nums[i];      } else {        upper.insert(nums[i]);        upperSum += nums[i];      }      if (i >= x) {        const int outNum = nums[i - x];        if (const auto it = lower.find(outNum); it != lower.cend()) {          lower.erase(it);          lowerSum -= outNum;        } else {          upper.erase(upper.find(outNum));          upperSum -= outNum;        }      }      // Balance the two multisets s.t.      // |lower| >= |upper| and |lower| - |upper| <= 1.      if (lower.size() < upper.size()) {        const int val = *upper.begin();        upper.erase(upper.begin());        lower.insert(val);        upperSum -= val;        lowerSum += val;      } else if (lower.size() - upper.size() > 1) {        const int val = *lower.rbegin();        lower.erase(prev(lower.end()));        upper.insert(val);        lowerSum -= val;        upperSum += val;      }      // Calculate operations needed to make all elements in the window equal      // to the median.      if (i >= x - 1) {        const int median = *lower.rbegin();        const long ops = (median * lower.size() - lowerSum) +                         (upperSum - median * upper.size());        minOps.push_back(ops);      }    }    return minOps;  }}; 

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 ↗