-
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 1879: Minimum XOR Sum of Two Arrays
- Loading branch information
Showing
3 changed files
with
70 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
51 changes: 51 additions & 0 deletions
51
src/problem_1879_minimum_xor_sum_of_two_arrays/dynamic_programming.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,51 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn minimum_xor_sum(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 { | ||
assert_eq!(nums1.len(), nums2.len()); | ||
|
||
let n = nums1.len(); | ||
let mut cache = vec![0_u32; 1 << n].into_boxed_slice(); | ||
|
||
for selection in 1_usize..cache.len() { | ||
let bottom_num = nums2[(selection.count_ones() - 1) as usize]; | ||
let mut value = u32::MAX; | ||
let mut remaining_bits = selection; | ||
|
||
loop { | ||
let top_index = remaining_bits.trailing_zeros(); | ||
let probe = 1 << top_index; | ||
|
||
value = value.min(cache[selection ^ probe] + (nums1[top_index as usize] ^ bottom_num) as u32); | ||
|
||
remaining_bits ^= probe; | ||
|
||
if remaining_bits == 0 { | ||
break; | ||
} | ||
} | ||
|
||
cache[selection] = value; | ||
} | ||
|
||
cache.last().copied().unwrap() as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn minimum_xor_sum(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 { | ||
Self::minimum_xor_sum(nums1, nums2) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
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,18 @@ | ||
pub mod dynamic_programming; | ||
|
||
pub trait Solution { | ||
fn minimum_xor_sum(nums1: Vec<i32>, nums2: Vec<i32>) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [((&[1, 2] as &[_], &[2, 3] as &[_]), 2), ((&[1, 0, 3], &[5, 3, 4]), 8)]; | ||
|
||
for ((nums1, nums2), expected) in test_cases { | ||
assert_eq!(S::minimum_xor_sum(nums1.to_vec(), nums2.to_vec()), expected); | ||
} | ||
} | ||
} |