|
| 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