2333. Minimum Sum of Squared Difference Medium
1/**
2 * [2333] Minimum Sum of Squared Difference
3 *
4 * You are given two positive 0-indexed integer arrays nums1 and nums2, both of length n.
5 * 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.
6 * 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.
7 * Return the minimum sum of squared difference after modifying array nums1 at most k1 times and modifying array nums2 at most k2 times.
8 * Note: You are allowed to modify the array elements to become negative integers.
9 *
10 * Example 1:
11 *
12 * Input: nums1 = [1,2,3,4], nums2 = [2,10,20,19], k1 = 0, k2 = 0
13 * Output: 579
14 * Explanation: The elements in nums1 and nums2 cannot be modified because k1 = 0 and k2 = 0.
15 * The sum of square difference will be: (1 - 2)^2 + (2 - 10)^2 + (3 - 20)^2 + (4 - 19)^2 = 579.
16 *
17 * Example 2:
18 *
19 * Input: nums1 = [1,4,10,12], nums2 = [5,8,6,9], k1 = 1, k2 = 1
20 * Output: 43
21 * Explanation: One way to obtain the minimum sum of square difference is:
22 * - Increase nums1[0] once.
23 * - Increase nums2[2] once.
24 * The minimum of the sum of square difference will be:
25 * (2 - 5)^2 + (4 - 8)^2 + (10 - 7)^2 + (12 - 9)^2 = 43.
26 * 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.
27 *
28 * Constraints:
29 *
30 * n == nums1.length == nums2.length
31 * 1 <= n <= 10^5
32 * 0 <= nums1[i], nums2[i] <= 10^5
33 * 0 <= k1, k2 <= 10^9
34 *
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/minimum-sum-of-squared-difference/
39// discuss: https://leetcode.com/problems/minimum-sum-of-squared-difference/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44 pub fn min_sum_square_diff(nums1: Vec<i32>, nums2: Vec<i32>, k1: i32, k2: i32) -> i64 {
45
46 }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn test_2333() {
57 }
58}
59
Back
© 2025 bowen.ge All Rights Reserved.