Problem solution · C++

Minimum Total Cost to Make Arrays Unequal

Minimum Total Cost to Make Arrays Unequal: 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
44 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 Total Cost to Make Arrays Unequal, 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

  • 44 lines of C++ from the credited upstream file 2499.cpp.
  • The implementation visibly relies on sequence storage.
  • 2 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 codeMinimum Total Cost to Make Arrays Unequal · C++C++
Use this to learn the idea, then write your own version.
class Solution { public:  long long minimumTotalCost(vector<int>& nums1, vector<int>& nums2) {    const int n = nums1.size();    long ans = 0;    int maxFreq = 0;    int maxFreqNum = 0;    int shouldBeSwapped = 0;    vector<int> conflictedNumCount(n + 1);     // Collect the indices i s.t. nums1[i] == nums2[i] and record their    // `maxFreq` and `maxFreqNum`.    for (int i = 0; i < n; ++i)      if (nums1[i] == nums2[i]) {        const int conflictedNum = nums1[i];        if (++conflictedNumCount[conflictedNum] > maxFreq) {          maxFreq = conflictedNumCount[conflictedNum];          maxFreqNum = conflictedNum;        }        ++shouldBeSwapped;        ans += i;      }     // Collect the indices with nums1[i] != nums2[i] that contribute less cost.    // This can be greedily achieved by iterating from 0 to n - 1.    for (int i = 0; i < n; ++i) {      // Since we have over `maxFreq * 2` spaces, `maxFreqNum` can be      // successfully distributed, so no need to collectextra spaces.      if (maxFreq * 2 <= shouldBeSwapped)        break;      if (nums1[i] == nums2[i])        continue;      // The numbers == `maxFreqNum` worsen the result since they increase the      // `maxFreq`.      if (nums1[i] == maxFreqNum || nums2[i] == maxFreqNum)        continue;      ++shouldBeSwapped;      ans += i;    }     return maxFreq * 2 > shouldBeSwapped ? -1 : ans;  }}; 

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 ↗