Skip to content

Commit e0dea9e

Browse files
committed
1775. Equal Sum Arrays With Minimum Number of Operations: AC
1 parent ebee9ca commit e0dea9e

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,3 +1338,4 @@ mod s1770_maximum_score_from_performing_multiplication_operations;
13381338
mod s1771_maximize_palindrome_length_from_subsequences;
13391339
mod s1773_count_items_matching_a_rule;
13401340
mod s1774_closest_dessert_cost;
1341+
mod s1775_equal_sum_arrays_with_minimum_number_of_operations;
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
*/
39+
pub 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+
46+
impl Solution {
47+
pub fn min_operations(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
48+
let a: i32 = nums1.iter().map(|&x| x as i32).sum();
49+
let b = nums2.iter().map(|&x| x as i32).sum();
50+
51+
if (a < b) {
52+
return Self::min_operations(nums2, nums1);
53+
}
54+
55+
let mut pq = std::collections::BinaryHeap::new();
56+
let mut amount = a - b;
57+
58+
for n in nums1 {
59+
if n > 1 {
60+
pq.push(n - 1);
61+
}
62+
}
63+
64+
for n in nums2 {
65+
if n < 6 {
66+
pq.push(6 - n);
67+
}
68+
}
69+
70+
let mut result = 0;
71+
72+
while amount > 0 && pq.is_empty() == false {
73+
amount -= pq.pop().unwrap();
74+
result += 1;
75+
}
76+
77+
if amount > 0 {
78+
return -1;
79+
}
80+
81+
result
82+
}
83+
}
84+
85+
// submission codes end
86+
87+
#[cfg(test)]
88+
mod tests {
89+
use super::*;
90+
91+
#[test]
92+
fn test_1775_example_1() {
93+
let nums1 = vec![1, 2, 3, 4, 5, 6];
94+
let nums2 = vec![1, 1, 2, 2, 2, 2];
95+
96+
let result = 3;
97+
98+
assert_eq!(Solution::min_operations(nums1, nums2), result);
99+
}
100+
101+
#[test]
102+
fn test_1775_example_2() {
103+
let nums1 = vec![1, 1, 1, 1, 1, 1, 1];
104+
let nums2 = vec![6];
105+
106+
let result = -1;
107+
108+
assert_eq!(Solution::min_operations(nums1, nums2), result);
109+
}
110+
111+
#[test]
112+
fn test_1775_example_3() {
113+
let nums1 = vec![6, 6];
114+
let nums2 = vec![1];
115+
116+
let result = 3;
117+
118+
assert_eq!(Solution::min_operations(nums1, nums2), result);
119+
}
120+
}

0 commit comments

Comments
 (0)