Skip to content

Commit 681bdee

Browse files
authored
2025-03-19 v. 9.0.3: added "2130. Maximum Twin Sum of a Linked List"
2 parents e4a180b + be43c3f commit 681bdee

4 files changed

+81
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
737737
| 2116. Check if a Parentheses String Can Be Valid | [Link](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/) | [Link](./lib/medium/2116_check_if_a_parentheses_string_can_be_valid.rb) | [Link](./test/medium/test_2116_check_if_a_parentheses_string_can_be_valid.rb) |
738738
| 2121. Intervals Between Identical Elements | [Link](https://leetcode.com/problems/intervals-between-identical-elements/) | [Link](./lib/medium/2121_intervals_between_identical_elements.rb) | [Link](./test/medium/test_2121_intervals_between_identical_elements.rb) |
739739
| 2125. Number of Laser Beams in a Bank | [Link](https://leetcode.com/problems/number-of-laser-beams-in-a-bank/) | [Link](./lib/medium/2125_number_of_laser_beams_in_a_bank.rb) | [Link](./test/medium/test_2125_number_of_laser_beams_in_a_bank.rb) |
740+
| 2130. Maximum Twin Sum of a Linked List | [Link](https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/) | [Link](./lib/medium/2130_maximum_twin_sum_of_a_linked_list.rb) | [Link](./test/medium/test_2130_maximum_twin_sum_of_a_linked_list.rb) |
740741
| 2425. Bitwise XOR of All Pairings | [Link](https://leetcode.com/problems/bitwise-xor-of-all-pairings/) | [Link](./lib/medium/2425_bitwise_xor_of_all_pairings.rb) | [Link](./test/medium/test_2425_bitwise_xor_of_all_pairings.rb) |
741742
| 2429. Minimize XOR | [Link](https://leetcode.com/problems/minimize-xor/) | [Link](./lib/medium/2429_minimize_xor.rb) | [Link](./test/medium/test_2429_minimize_xor.rb) |
742743
| 2657. Find the Prefix Common Array of Two Arrays | [Link](https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/) | [Link](./lib/medium/2657_find_the_prefix_common_array_of_two_arrays.rb) | [Link](./test/medium/test_2657_find_the_prefix_common_array_of_two_arrays.rb) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '9.0.2'
8+
s.version = '9.0.3'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/
4+
# @param {ListNode} head
5+
# @return {Integer}
6+
def pair_sum(head)
7+
slow = head
8+
fast = head
9+
first_part = head
10+
11+
while fast&.next
12+
first_part = slow
13+
slow = slow.next
14+
fast = fast.next.next
15+
end
16+
17+
prev = nil
18+
19+
while slow
20+
nxt = slow.next
21+
slow.next = prev
22+
prev = slow
23+
slow = nxt
24+
end
25+
26+
first_part.next = prev
27+
result = 0
28+
f = head
29+
s = first_part.next
30+
31+
while s
32+
result = [result, f.val + s.val].max
33+
f = f.next
34+
s = s.next
35+
end
36+
37+
result
38+
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/common/linked_list'
5+
require_relative '../../lib/medium/2130_maximum_twin_sum_of_a_linked_list'
6+
require 'minitest/autorun'
7+
8+
class MaximumTwinSumOfALinkedListTest < ::Minitest::Test
9+
def test_default_one
10+
assert_equal(
11+
6,
12+
pair_sum(
13+
::ListNode.from_array(
14+
[5, 4, 2, 1]
15+
)
16+
)
17+
)
18+
end
19+
20+
def test_default_two
21+
assert_equal(
22+
7,
23+
pair_sum(
24+
::ListNode.from_array(
25+
[4, 2, 2, 3]
26+
)
27+
)
28+
)
29+
end
30+
31+
def test_default_three
32+
assert_equal(
33+
100_001,
34+
pair_sum(
35+
::ListNode.from_array(
36+
[1, 100_000]
37+
)
38+
)
39+
)
40+
end
41+
end

0 commit comments

Comments
 (0)