diff --git a/src/lib.rs b/src/lib.rs index 1b3de118..f38c9fd5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1442,6 +1442,7 @@ pub mod problem_1861_rotating_the_box; pub mod problem_1864_minimum_number_of_swaps_to_make_the_binary_string_alternating; pub mod problem_1869_longer_contiguous_segments_of_ones_than_zeros; pub mod problem_1876_substrings_of_size_three_with_distinct_characters; +pub mod problem_1877_minimize_maximum_pair_sum_in_array; #[cfg(test)] mod test_utilities; diff --git a/src/problem_1877_minimize_maximum_pair_sum_in_array/greedy.rs b/src/problem_1877_minimize_maximum_pair_sum_in_array/greedy.rs new file mode 100644 index 00000000..e8bad8ba --- /dev/null +++ b/src/problem_1877_minimize_maximum_pair_sum_in_array/greedy.rs @@ -0,0 +1,38 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + pub fn min_pair_sum(nums: Vec) -> i32 { + let mut nums = nums; + + nums.sort_unstable(); + + let mut nums = nums.as_slice(); + let mut result = i32::MIN; + + while let [first, rest @ .., last] = nums { + result = result.max(first + last); + + nums = rest; + } + + result + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn min_pair_sum(nums: Vec) -> i32 { + Self::min_pair_sum(nums) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_1877_minimize_maximum_pair_sum_in_array/mod.rs b/src/problem_1877_minimize_maximum_pair_sum_in_array/mod.rs new file mode 100644 index 00000000..07a422a0 --- /dev/null +++ b/src/problem_1877_minimize_maximum_pair_sum_in_array/mod.rs @@ -0,0 +1,18 @@ +pub mod greedy; + +pub trait Solution { + fn min_pair_sum(nums: Vec) -> i32; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [(&[3, 5, 2, 3] as &[_], 7), (&[3, 5, 4, 2, 4, 6], 8)]; + + for (nums, expected) in test_cases { + assert_eq!(S::min_pair_sum(nums.to_vec()), expected); + } + } +}