You are given two positive 0-indexed integer arrays nums1 and nums2, both of length n.
The sum of squared difference of arrays nums1 and nums2 is defined as the sum of (nums1[i] - nums2[i])2 for each 0 <= i < n.
You are also given two positive integers k1 and k2. You can modify any of the elements of nums1 by +1 or -1 at most k1 times. Similarly, you can modify any of the elements of nums2 by +1 or -1 at most k2 times.
Return _the minimumsum of squared difference after modifying array _nums1at mostk1times and modifying arraynums2at mostk2times.
Note : You are allowed to modify the array elements to become negative integers.
Input: nums1 =[1,2,3,4], nums2 =[2,10,20,19], k1 =0, k2 =0Output: 579Explanation: The elements in nums1 and nums2 cannot be modified because k1 =0 and k2 =0.The sum of square difference will be:(1-2)2+(2-10)2+(3-20)2+(4-19)2=579.
Input: nums1 =[1,4,10,12], nums2 =[5,8,6,9], k1 =1, k2 =1Output: 43Explanation: One way to obtain the minimum sum of square difference is:- Increase nums1[0] once.- Increase nums2[2] once.The minimum of the sum of square difference will be:(2-5)2+(4-8)2+(10-7)2+(12-9)2=43.Note that, there are other ways to obtain the minimum of the sum of square difference, but there is no way to obtain a sum smaller than 43.
To minimize the sum of squared differences, we should reduce the largest differences first. We can use a frequency array to count the occurrences of each difference, then greedily reduce the largest differences using available operations.
classSolution {
public:longlong minSumSquareDiff(vector<int>& nums1, vector<int>& nums2, int k1, int k2) {
int n = nums1.size(), k = k1 + k2;
vector<int> diff(n);
for (int i =0; i < n; ++i) diff[i] = abs(nums1[i] - nums2[i]);
vector<longlong> freq(100001, 0);
for (int d : diff) freq[d]++;
for (int i =100000; i >0&& k >0; --i) {
longlong take = min((longlong)freq[i], (longlong)k);
freq[i] -= take;
freq[i-1] += take;
k -= take;
}
longlong ans =0;
for (int i =0; i <=100000; ++i) ans += freq[i] * i * i;
return ans;
}
};
classSolution {
publiclongminSumSquareDiff(int[] nums1, int[] nums2, int k1, int k2) {
int n = nums1.length, k = k1 + k2;
long[] freq =newlong[100001];
for (int i = 0; i < n; i++) freq[Math.abs(nums1[i]-nums2[i])]++;
for (int i = 100000; i > 0 && k > 0; i--) {
long take = Math.min(freq[i], k);
freq[i]-= take;
freq[i-1]+= take;
k -= take;
}
long ans = 0;
for (int i = 0; i <= 100000; i++) ans += freq[i]* i * i;
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
funminSumSquareDiff(nums1: IntArray, nums2: IntArray, k1: Int, k2: Int): Long {
val n = nums1.size; var k = k1 + k2
val freq = LongArray(100001)
for (i in0 until n) freq[kotlin.math.abs(nums1[i]-nums2[i])]++for (i in100000 downTo 1) {
if (k ==0) breakval take = minOf(freq[i], k.toLong())
freq[i] -= take
freq[i-1] += take
k -= take.toInt()
}
var ans = 0Lfor (i in0..100000) ans += freq[i] * i * i
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution:
defminSumSquareDiff(self, nums1: list[int], nums2: list[int], k1: int, k2: int) -> int:
from collections import Counter
n, k = len(nums1), k1 + k2
diff = [abs(a-b) for a,b in zip(nums1, nums2)]
freq = Counter(diff)
for i in range(100000, 0, -1):
take = min(freq[i], k)
freq[i] -= take
freq[i-1] += take
k -= take
return sum(v*i*i for i,v in freq.items())
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
impl Solution {
pubfnmin_sum_square_diff(nums1: Vec<i32>, nums2: Vec<i32>, k1: i32, k2: i32) -> i64 {
let n = nums1.len(); letmut k = k1 + k2;
letmut freq =vec![0i64; 100001];
for i in0..n { freq[(nums1[i]-nums2[i]).abs() asusize] +=1; }
for i in (1..=100000).rev() {
if k ==0 { break; }
let take = freq[i].min(k asi64);
freq[i] -= take;
freq[i-1] += take;
k -= take asi32;
}
letmut ans =0i64;
for i in0..=100000 { ans += freq[i] * (i asi64) * (i asi64); }
ans
}
}