-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2333-minimum-sum-of-squared-difference.js
More file actions
84 lines (73 loc) · 2.44 KB
/
2333-minimum-sum-of-squared-difference.js
File metadata and controls
84 lines (73 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* 2333. Minimum Sum of Squared Difference
* https://leetcode.com/problems/minimum-sum-of-squared-difference/
* Difficulty: Medium
*
* 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 minimum sum of squared difference after modifying array nums1 at most k1
* times and modifying array nums2 at most k2 times.
*
* Note: You are allowed to modify the array elements to become negative integers.
*/
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @param {number} k1
* @param {number} k2
* @return {number}
*/
var minSumSquareDiff = function(nums1, nums2, k1, k2) {
const n = nums1.length;
const frequencyMap = new Map();
for (let i = 0; i < n; i++) {
const diff = Math.abs(nums1[i] - nums2[i]);
frequencyMap.set(diff, (frequencyMap.get(diff) || 0) + 1);
}
let totalMoves = k1 + k2;
const maxHeap = new PriorityQueue((a, b) => b[0] - a[0]);
for (const [value, count] of frequencyMap) {
if (value === 0) continue;
maxHeap.enqueue([value, count]);
}
while (!maxHeap.isEmpty() && totalMoves > 0) {
const [value, count] = maxHeap.dequeue();
if (maxHeap.isEmpty()) {
const moves = Math.min(totalMoves, count);
totalMoves -= moves;
const remainingCount = count - moves;
if (value - 1 > 0) {
maxHeap.enqueue([value - 1, moves]);
}
if (remainingCount > 0) {
maxHeap.enqueue([value, remainingCount]);
}
} else {
const moves = Math.min(totalMoves, count);
totalMoves -= moves;
const remainingCount = count - moves;
if (remainingCount > 0) {
maxHeap.enqueue([value, remainingCount]);
}
if (!maxHeap.isEmpty() && maxHeap.front()[0] === value - 1) {
const [nextValue, nextCount] = maxHeap.dequeue();
maxHeap.enqueue([nextValue, nextCount + moves]);
} else if (value - 1 > 0) {
maxHeap.enqueue([value - 1, moves]);
}
}
}
let result = 0;
while (!maxHeap.isEmpty()) {
const [value, count] = maxHeap.dequeue();
result += value * value * count;
}
return result;
};