1775. Equal Sum Arrays With Minimum Number of Operations Medium
1/**
2 * [1775] Equal Sum Arrays With Minimum Number of Operations
3 *
4 * You are given two arrays of integers nums1 and <font face="monospace">nums2</font>, possibly of different lengths. The values in the arrays are between 1 and 6, inclusive.
5 * In one operation, you can change any integer's value in any of the arrays to any value between 1 and 6, inclusive.
6 * Return the minimum number of operations required to make the sum of values in nums1 equal to the sum of values in nums2. Return -1 if it is not possible to make the sum of the two arrays equal.
7 *
8 * Example 1:
9 *
10 * Input: nums1 = [1,2,3,4,5,6], nums2 = [1,1,2,2,2,2]
11 * Output: 3
12 * Explanation: You can make the sums of nums1 and nums2 equal with 3 operations. All indices are 0-indexed.
13 * - Change nums2[0] to 6. nums1 = [1,2,3,4,5,6], nums2 = [<u>6</u>,1,2,2,2,2].
14 * - Change nums1[5] to 1. nums1 = [1,2,3,4,5,<u>1</u>], nums2 = [6,1,2,2,2,2].
15 * - Change nums1[2] to 2. nums1 = [1,2,<u>2</u>,4,5,1], nums2 = [6,1,2,2,2,2].
16 *
17 * Example 2:
18 *
19 * Input: nums1 = [1,1,1,1,1,1,1], nums2 = [6]
20 * Output: -1
21 * Explanation: There is no way to decrease the sum of nums1 or to increase the sum of nums2 to make them equal.
22 *
23 * Example 3:
24 *
25 * Input: nums1 = [6,6], nums2 = [1]
26 * Output: 3
27 * Explanation: You can make the sums of nums1 and nums2 equal with 3 operations. All indices are 0-indexed.
28 * - Change nums1[0] to 2. nums1 = [<u>2</u>,6], nums2 = [1].
29 * - Change nums1[1] to 2. nums1 = [2,<u>2</u>], nums2 = [1].
30 * - Change nums2[0] to 4. nums1 = [2,2], nums2 = [<u>4</u>].
31 *
32 *
33 * Constraints:
34 *
35 * 1 <= nums1.length, nums2.length <= 10^5
36 * 1 <= nums1[i], nums2[i] <= 6
37 *
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/equal-sum-arrays-with-minimum-number-of-operations/
42// discuss: https://leetcode.com/problems/equal-sum-arrays-with-minimum-number-of-operations/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47 pub fn min_operations(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
48 0
49 }
50}
51
52// submission codes end
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn test_1775() {
60 }
61}
62
Back
© 2025 bowen.ge All Rights Reserved.