From 6035b5501445a4a5218d11cbcc5af6ea9f671de2 Mon Sep 17 00:00:00 2001 From: EFanZh Date: Wed, 7 Feb 2024 22:36:11 +0800 Subject: [PATCH] Add problem 2130: Maximum Twin Sum of a Linked List --- src/lib.rs | 1 + .../in_place.rs | 37 +++++++++++++ .../mod.rs | 22 ++++++++ .../reverse_half.rs | 54 +++++++++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 src/problem_2130_maximum_twin_sum_of_a_linked_list/in_place.rs create mode 100644 src/problem_2130_maximum_twin_sum_of_a_linked_list/mod.rs create mode 100644 src/problem_2130_maximum_twin_sum_of_a_linked_list/reverse_half.rs diff --git a/src/lib.rs b/src/lib.rs index 445b7729..e944617b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1441,6 +1441,7 @@ pub mod problem_2114_maximum_number_of_words_found_in_sentences; pub mod problem_2119_a_number_after_a_double_reversal; pub mod problem_2124_check_if_all_as_appears_before_all_bs; pub mod problem_2129_capitalize_the_title; +pub mod problem_2130_maximum_twin_sum_of_a_linked_list; #[cfg(test)] mod test_utilities; diff --git a/src/problem_2130_maximum_twin_sum_of_a_linked_list/in_place.rs b/src/problem_2130_maximum_twin_sum_of_a_linked_list/in_place.rs new file mode 100644 index 00000000..6b5fb2e7 --- /dev/null +++ b/src/problem_2130_maximum_twin_sum_of_a_linked_list/in_place.rs @@ -0,0 +1,37 @@ +use crate::data_structures::ListNode; + +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + pub fn pair_sum(head: Option>) -> i32 { + let mut head = head; + let mut list = Vec::new(); + + while let Some(node) = head { + list.push(node.val); + head = node.next; + } + + let (left, right) = list.split_at(list.len() / 2); + + left.iter().zip(right.iter().rev()).map(|(x, y)| x + y).max().unwrap() + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn pair_sum(head: Option>) -> i32 { + Self::pair_sum(head) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_2130_maximum_twin_sum_of_a_linked_list/mod.rs b/src/problem_2130_maximum_twin_sum_of_a_linked_list/mod.rs new file mode 100644 index 00000000..aba7ce8e --- /dev/null +++ b/src/problem_2130_maximum_twin_sum_of_a_linked_list/mod.rs @@ -0,0 +1,22 @@ +use crate::data_structures::ListNode; + +pub mod in_place; +pub mod reverse_half; + +pub trait Solution { + fn pair_sum(head: Option>) -> i32; +} + +#[cfg(test)] +mod tests { + use super::Solution; + use crate::test_utilities; + + pub fn run() { + let test_cases = [(&[5, 4, 2, 1] as &[_], 6), (&[4, 2, 2, 3], 7), (&[1, 100_000], 100_001)]; + + for (head, expected) in test_cases { + assert_eq!(S::pair_sum(test_utilities::make_list(head.iter().copied())), expected); + } + } +} diff --git a/src/problem_2130_maximum_twin_sum_of_a_linked_list/reverse_half.rs b/src/problem_2130_maximum_twin_sum_of_a_linked_list/reverse_half.rs new file mode 100644 index 00000000..ec7dba57 --- /dev/null +++ b/src/problem_2130_maximum_twin_sum_of_a_linked_list/reverse_half.rs @@ -0,0 +1,54 @@ +use crate::data_structures::ListNode; + +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +use std::iter; +use std::mem; + +impl Solution { + fn make_iter(list: Option<&ListNode>) -> impl Iterator { + iter::successors(list, |node| node.next.as_deref()) + } + + pub fn pair_sum(head: Option>) -> i32 { + let mut head = head; + let length = Self::make_iter(head.as_deref()).count(); + let mut node = &mut head; + + for _ in 0..length / 2 { + node = &mut node.as_deref_mut().unwrap().next; + } + + let mut right = None; + let mut maybe_node = node.take(); + + while let Some(mut node) = maybe_node { + maybe_node = mem::replace(&mut node.next, right); + right = Some(node); + } + + Self::make_iter(head.as_deref()) + .zip(Self::make_iter(right.as_deref())) + .map(|(left, right)| left.val + right.val) + .max() + .unwrap() + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn pair_sum(head: Option>) -> i32 { + Self::pair_sum(head) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +}