Skip to content

Commit f92585b

Browse files
committed
Add solution #2333
1 parent 3797e1e commit f92585b

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2117,6 +2117,7 @@
21172117
2330|[Valid Palindrome IV](./solutions/2330-valid-palindrome-iv.js)|Medium|
21182118
2331|[Evaluate Boolean Binary Tree](./solutions/2331-evaluate-boolean-binary-tree.js)|Easy|
21192119
2332|[The Latest Time to Catch a Bus](./solutions/2332-the-latest-time-to-catch-a-bus.js)|Medium|
2120+
2333|[Minimum Sum of Squared Difference](./solutions/2333-minimum-sum-of-squared-difference.js)|Medium|
21202121
2334|[Subarray With Elements Greater Than Varying Threshold](./solutions/2334-subarray-with-elements-greater-than-varying-threshold.js)|Hard|
21212122
2335|[Minimum Amount of Time to Fill Cups](./solutions/2335-minimum-amount-of-time-to-fill-cups.js)|Easy|
21222123
2336|[Smallest Number in Infinite Set](./solutions/2336-smallest-number-in-infinite-set.js)|Medium|
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* 2333. Minimum Sum of Squared Difference
3+
* https://leetcode.com/problems/minimum-sum-of-squared-difference/
4+
* Difficulty: Medium
5+
*
6+
* You are given two positive 0-indexed integer arrays nums1 and nums2, both of length n.
7+
*
8+
* The sum of squared difference of arrays nums1 and nums2 is defined as the sum of
9+
* (nums1[i] - nums2[i])2 for each 0 <= i < n.
10+
*
11+
* You are also given two positive integers k1 and k2. You can modify any of the elements
12+
* of nums1 by +1 or -1 at most k1 times. Similarly, you can modify any of the elements
13+
* of nums2 by +1 or -1 at most k2 times.
14+
*
15+
* Return the minimum sum of squared difference after modifying array nums1 at most k1
16+
* times and modifying array nums2 at most k2 times.
17+
*
18+
* Note: You are allowed to modify the array elements to become negative integers.
19+
*/
20+
21+
/**
22+
* @param {number[]} nums1
23+
* @param {number[]} nums2
24+
* @param {number} k1
25+
* @param {number} k2
26+
* @return {number}
27+
*/
28+
var minSumSquareDiff = function(nums1, nums2, k1, k2) {
29+
const n = nums1.length;
30+
const frequencyMap = new Map();
31+
32+
for (let i = 0; i < n; i++) {
33+
const diff = Math.abs(nums1[i] - nums2[i]);
34+
frequencyMap.set(diff, (frequencyMap.get(diff) || 0) + 1);
35+
}
36+
37+
let totalMoves = k1 + k2;
38+
const maxHeap = new PriorityQueue((a, b) => b[0] - a[0]);
39+
40+
for (const [value, count] of frequencyMap) {
41+
if (value === 0) continue;
42+
maxHeap.enqueue([value, count]);
43+
}
44+
45+
while (!maxHeap.isEmpty() && totalMoves > 0) {
46+
const [value, count] = maxHeap.dequeue();
47+
48+
if (maxHeap.isEmpty()) {
49+
const moves = Math.min(totalMoves, count);
50+
totalMoves -= moves;
51+
const remainingCount = count - moves;
52+
53+
if (value - 1 > 0) {
54+
maxHeap.enqueue([value - 1, moves]);
55+
}
56+
if (remainingCount > 0) {
57+
maxHeap.enqueue([value, remainingCount]);
58+
}
59+
} else {
60+
const moves = Math.min(totalMoves, count);
61+
totalMoves -= moves;
62+
const remainingCount = count - moves;
63+
64+
if (remainingCount > 0) {
65+
maxHeap.enqueue([value, remainingCount]);
66+
}
67+
68+
if (!maxHeap.isEmpty() && maxHeap.front()[0] === value - 1) {
69+
const [nextValue, nextCount] = maxHeap.dequeue();
70+
maxHeap.enqueue([nextValue, nextCount + moves]);
71+
} else if (value - 1 > 0) {
72+
maxHeap.enqueue([value - 1, moves]);
73+
}
74+
}
75+
}
76+
77+
let result = 0;
78+
while (!maxHeap.isEmpty()) {
79+
const [value, count] = maxHeap.dequeue();
80+
result += value * value * count;
81+
}
82+
83+
return result;
84+
};

0 commit comments

Comments
 (0)