Equal Sum Arrays With Minimum Number of Operations LeetCode solution in Rust

1
use std::collections::BinaryHeap;
2

3
impl Solution {
4
pub fn min_operations(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
5
let a: i32 = nums1.iter().map(|&x| x as i32).sum();
6
let b = nums2.iter().map(|&x| x as i32).sum();
7
if (a < b) {
8
return Self::min_operations(nums2, nums1);
9
}
10

11
let mut pq = BinaryHeap::new();
12
let mut amt = a - b;
13

14
for n in nums1 {
15
if n > 1 {
16
pq.push(n - 1);
17
}
18
}
19

20
for n in nums2 {
21
if n < 6 {
22
pq.push(6 - n);
23
}
24
}
25

26
let mut ret = 0;
27
while amt > 0 && pq.is_empty() == false {
28
amt -= pq.pop().unwrap();
29
ret += 1;
30
}
31

32
if amt > 0 {
33
return -1;
34
}
35
ret
36
}
37
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0