-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1775: Equal Sum Arrays With Minimum Number of Operations
- Loading branch information
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/problem_1775_equal_sum_arrays_with_minimum_number_of_operations/greedy.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::mem; | ||
|
||
impl Solution { | ||
fn count_nums(nums: Vec<i32>) -> [u32; 6] { | ||
let mut result = [0; 6]; | ||
|
||
for num in nums { | ||
result[num as u32 as usize - 1] += 1; | ||
} | ||
|
||
result | ||
} | ||
|
||
fn calculate_sum(counts: &[u32; 6]) -> u32 { | ||
let mut result = 0; | ||
let mut i = 0; | ||
|
||
for count in counts { | ||
i += 1; | ||
result += i * count; | ||
} | ||
|
||
result | ||
} | ||
|
||
pub fn min_operations(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 { | ||
let mut counts_1 = Self::count_nums(nums1); | ||
let sums_1 = Self::calculate_sum(&counts_1); | ||
let mut counts_2 = Self::count_nums(nums2); | ||
let sums_2 = Self::calculate_sum(&counts_2); | ||
|
||
let mut diff = if sums_2 < sums_1 { | ||
mem::swap(&mut counts_1, &mut counts_2); | ||
|
||
sums_1 - sums_2 | ||
} else { | ||
sums_2 - sums_1 | ||
}; | ||
|
||
let mut iter = counts_1.iter().zip(counts_2.iter().rev()).map(|(lhs, rhs)| lhs + rhs); | ||
let mut gain = 6; | ||
let mut result = 0; | ||
|
||
while diff != 0 { | ||
if let Some(count) = iter.next() { | ||
gain -= 1; | ||
|
||
if let Some(required) = (diff + gain - 1).checked_div(gain) { | ||
if count < required { | ||
result += count; | ||
|
||
diff -= gain * count; | ||
} else { | ||
result += required; | ||
|
||
break; | ||
} | ||
} | ||
} else { | ||
return -1; | ||
} | ||
} | ||
|
||
result as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn min_operations(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 { | ||
Self::min_operations(nums1, nums2) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/problem_1775_equal_sum_arrays_with_minimum_number_of_operations/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
pub mod greedy; | ||
|
||
pub trait Solution { | ||
fn min_operations(nums1: Vec<i32>, nums2: Vec<i32>) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
((&[1, 2, 3, 4, 5, 6] as &[_], &[1, 1, 2, 2, 2, 2] as &[_]), 3), | ||
((&[1, 1, 1, 1, 1, 1, 1], &[6]), -1), | ||
((&[6, 6], &[1]), 3), | ||
]; | ||
|
||
for ((nums1, nums2), expected) in test_cases { | ||
assert_eq!(S::min_operations(nums1.to_vec(), nums2.to_vec()), expected); | ||
} | ||
} | ||
} |