-
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 2035: Partition Array Into Two Arrays to Minimize Sum Dif…
…ference
- Loading branch information
Showing
3 changed files
with
86 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
63 changes: 63 additions & 0 deletions
63
...lem_2035_partition_array_into_two_arrays_to_minimize_sum_difference/meet_in_the_middle.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,63 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
fn get_sums(nums: &[i32], sums: &mut [i32]) -> Box<[Vec<i32>]> { | ||
let n = nums.len(); | ||
let mut result = vec![Vec::new(); n + 1].into_boxed_slice(); | ||
|
||
result[0].push(0); | ||
|
||
for selected in 1..sums.len() { | ||
let sum = sums[selected & (selected - 1)] + nums[selected.trailing_zeros() as usize]; | ||
|
||
sums[selected] = sum; | ||
result[selected.count_ones() as usize].push(sum); | ||
} | ||
|
||
result | ||
} | ||
|
||
pub fn minimum_difference(nums: Vec<i32>) -> i32 { | ||
let (left, right) = nums.split_at(nums.len() / 2); | ||
let mut sums = vec![0; 1 << left.len()].into_boxed_slice(); | ||
let mut left_sums = Self::get_sums(left, &mut sums); | ||
let mut right_sums = Self::get_sums(right, &mut sums); | ||
let get_last = |sums: &[Vec<i32>]| *sums.last().and_then(|sums| sums.first()).unwrap(); | ||
let sum = get_last(&left_sums) + get_last(&right_sums); | ||
let half = sum.div_euclid(2); | ||
let mut min_sum = u32::MAX; | ||
|
||
for (left_sums, right_sums) in left_sums.iter_mut().rev().zip(&mut *right_sums) { | ||
right_sums.sort_unstable(); | ||
|
||
for &left_sum in &*left_sums { | ||
let target = half - left_sum; | ||
let i = right_sums.partition_point(|&x| x < target); | ||
|
||
if let Some(&right_sum) = right_sums.get(i) { | ||
min_sum = u32::min(min_sum, ((left_sum + right_sum) * 2 - sum) as _); | ||
} | ||
} | ||
} | ||
|
||
min_sum as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn minimum_difference(nums: Vec<i32>) -> i32 { | ||
Self::minimum_difference(nums) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/problem_2035_partition_array_into_two_arrays_to_minimize_sum_difference/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 meet_in_the_middle; | ||
|
||
pub trait Solution { | ||
fn minimum_difference(nums: Vec<i32>) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&[3, 9, 7, 3] as &[_], 2), | ||
(&[-36, 36], 72), | ||
(&[2, -1, 0, 4, -2, -9], 0), | ||
]; | ||
|
||
for (nums, expected) in test_cases { | ||
assert_eq!(S::minimum_difference(nums.to_vec()), expected); | ||
} | ||
} | ||
} |