LeetCode Entry
2130. Maximum Twin Sum of a Linked List
Max sum pairwise from tail
2130. Maximum Twin Sum of a Linked List medium substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1390
Problem TLDR
Max sum pairwise from tail
Intuition
Put into a list or revert the first half.
Approach
- Kotlin: generateSequence
- Rust: from_fn
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(n|1)\)
Code
fun pairSum(h: ListNode?) =
generateSequence(h){it.next}.map{it.`val`}
.toList().run {zip(reversed(), Int::plus).max()}
pub fn pair_sum(mut h: Option<Box<ListNode>>) -> i32 {
let l: Vec<_> = from_fn(||h.take().map(|n|{h=n.next; n.val})).collect();
l.iter().rev().zip(&l).map(|(a,b)|a+b).max().unwrap()
}
Comments