Problem Description
Given two integer arrays nums1 and nums2 of equal length, and two integers k1 and k2 representing the maximum number of operations allowed on nums1 and nums2 respectively, an operation being an increment or decrement by one, the objective is to minimize the sum of squared differences between corresponding elements of the two arrays. The sum of squared difference is defined as Σ (nums1[i] - nums2[i])².
Key Insights
- The absolute difference at each index, diff[i] = |nums1[i] - nums2[i]|, is the target for reduction.
- The total number of available operations is k = k1 + k2; each operation reduces one unit from some diff.
- Optimal strategy is to greedily reduce the largest differences first since reducing a larger diff by one typically produces more benefit in terms of decreasing its square.
- Using a frequency “bucket” that counts how many elements have each possible difference (from 0 up to maxDiff) enables efficient simulation of operations.
Space and Time Complexity
Time Complexity: O(n + D) where n is the length of the arrays and D is the maximum difference (up to 10^5).
Space Complexity: O(D) for storing the frequency bucket.
Solution
We start by computing the absolute difference for each pair (nums1[i], nums2[i]) and count them in a frequency bucket indexed by the difference value. We then combine the available operations (k = k1 + k2) and simulate reducing the differences from the highest value downward. For a given difference d with a count freq[d]:
- If the number of available operations is at least the count, we reduce all occurrences by one (i.e. move them to bucket index d-1) and subtract that count from k.
- If fewer operations are available than the count, we reduce as many as possible from d to d-1 and stop.
After processing all the operations (or if all differences become 0), we compute the final sum of squared differences as Σ (d² * freq[d]).
This bucket simulation ensures we are always reducing the highest differences first, thereby minimizing the total squared sum.