Skip to content

2025-03-19 v. 9.0.3: added "2130. Maximum Twin Sum of a Linked List" #992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
Expand Down
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '9.0.2'
s.version = '9.0.3'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
38 changes: 38 additions & 0 deletions lib/medium/2130_maximum_twin_sum_of_a_linked_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

# https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/
# @param {ListNode} head
# @return {Integer}
def pair_sum(head)
slow = head
fast = head
first_part = head

while fast&.next
first_part = slow
slow = slow.next
fast = fast.next.next
end

prev = nil

while slow
nxt = slow.next
slow.next = prev
prev = slow
slow = nxt
end

first_part.next = prev
result = 0
f = head
s = first_part.next

while s
result = [result, f.val + s.val].max
f = f.next
s = s.next
end

result
end
41 changes: 41 additions & 0 deletions test/medium/test_2130_maximum_twin_sum_of_a_linked_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/common/linked_list'
require_relative '../../lib/medium/2130_maximum_twin_sum_of_a_linked_list'
require 'minitest/autorun'

class MaximumTwinSumOfALinkedListTest < ::Minitest::Test
def test_default_one
assert_equal(
6,
pair_sum(
::ListNode.from_array(
[5, 4, 2, 1]
)
)
)
end

def test_default_two
assert_equal(
7,
pair_sum(
::ListNode.from_array(
[4, 2, 2, 3]
)
)
)
end

def test_default_three
assert_equal(
100_001,
pair_sum(
::ListNode.from_array(
[1, 100_000]
)
)
)
end
end