Skip to content

Commit 1d440d2

Browse files
committed
2130. Maximum Twin Sum of a Linked List: RETRY
1 parent 9e04e6c commit 1d440d2

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,3 +1608,4 @@ mod s2125_number_of_laser_beams_in_a_bank;
16081608
mod s2126_destroying_asteroids;
16091609
mod s2127_maximum_employees_to_be_invited_to_a_meeting;
16101610
mod s2129_capitalize_the_title;
1611+
mod s2130_maximum_twin_sum_of_a_linked_list;
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* [2130] Maximum Twin Sum of a Linked List
3+
*
4+
* In a linked list of size n, where n is even, the i^th node (0-indexed) of the linked list is known as the twin of the (n-1-i)^th node, if 0 <= i <= (n / 2) - 1.
5+
*
6+
* For example, if n = 4, then node 0 is the twin of node 3, and node 1 is the twin of node 2. These are the only nodes with twins for n = 4.
7+
*
8+
* The twin sum is defined as the sum of a node and its twin.
9+
* Given the head of a linked list with even length, return the maximum twin sum of the linked list.
10+
*
11+
* Example 1:
12+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/12/03/eg1drawio.png" style="width: 250px; height: 70px;" />
13+
* Input: head = [5,4,2,1]
14+
* Output: 6
15+
* Explanation:
16+
* Nodes 0 and 1 are the twins of nodes 3 and 2, respectively. All have twin sum = 6.
17+
* There are no other nodes with twins in the linked list.
18+
* Thus, the maximum twin sum of the linked list is 6.
19+
*
20+
* Example 2:
21+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/12/03/eg2drawio.png" style="width: 250px; height: 70px;" />
22+
* Input: head = [4,2,2,3]
23+
* Output: 7
24+
* Explanation:
25+
* The nodes with twins present in this linked list are:
26+
* - Node 0 is the twin of node 3 having a twin sum of 4 + 3 = 7.
27+
* - Node 1 is the twin of node 2 having a twin sum of 2 + 2 = 4.
28+
* Thus, the maximum twin sum of the linked list is max(7, 4) = 7.
29+
*
30+
* Example 3:
31+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/12/03/eg3drawio.png" style="width: 200px; height: 88px;" />
32+
* Input: head = [1,100000]
33+
* Output: 100001
34+
* Explanation:
35+
* There is only one node with a twin in the linked list having twin sum of 1 + 100000 = 100001.
36+
*
37+
*
38+
* Constraints:
39+
*
40+
* The number of nodes in the list is an even integer in the range [2, 10^5].
41+
* 1 <= Node.val <= 10^5
42+
*
43+
*/
44+
pub struct Solution {}
45+
use crate::util::linked_list::{to_list, ListNode};
46+
47+
// problem: https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/
48+
// discuss: https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/discuss/?currentPage=1&orderBy=most_votes&query=
49+
50+
// submission codes start here
51+
52+
// Definition for singly-linked list.
53+
// #[derive(PartialEq, Eq, Clone, Debug)]
54+
// pub struct ListNode {
55+
// pub val: i32,
56+
// pub next: Option<Box<ListNode>>
57+
// }
58+
//
59+
// impl ListNode {
60+
// #[inline]
61+
// fn new(val: i32) -> Self {
62+
// ListNode {
63+
// next: None,
64+
// val
65+
// }
66+
// }
67+
// }
68+
impl Solution {
69+
pub fn pair_sum(head: Option<Box<ListNode>>) -> i32 {
70+
0
71+
}
72+
}
73+
74+
// submission codes end
75+
76+
#[cfg(test)]
77+
mod tests {
78+
use super::*;
79+
80+
#[test]
81+
#[ignore]
82+
fn test_2130_example_1() {
83+
let head = linked![5, 4, 2, 1];
84+
85+
let result = 6;
86+
87+
assert_eq!(Solution::pair_sum(head), result);
88+
}
89+
90+
#[test]
91+
#[ignore]
92+
fn test_2130_example_2() {
93+
let head = linked![4, 2, 2, 3];
94+
95+
let result = 7;
96+
97+
assert_eq!(Solution::pair_sum(head), result);
98+
}
99+
100+
#[test]
101+
#[ignore]
102+
fn test_2130_example_3() {
103+
let head = linked![1, 100000];
104+
105+
let result = 100001;
106+
107+
assert_eq!(Solution::pair_sum(head), result);
108+
}
109+
}

0 commit comments

Comments
 (0)