Skip to content
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 @@ -615,3 +615,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 707. Design Linked List | [Link](https://leetcode.com/problems/design-linked-list/) | [Link](./lib/medium/707_design_linked_list.rb) | [Link](./test/medium/test_707_design_linked_list.rb) |
| 713. Subarray Product Less Than K | [Link](https://leetcode.com/problems/subarray-product-less-than-k/) | [Link](./lib/medium/713_subarray_product_less_than_k.rb) | [Link](./test/medium/test_713_subarray_product_less_than_k.rb) |
| 720. Longest Word in Dictionary | [Link](https://leetcode.com/problems/longest-word-in-dictionary/) | [Link](./lib/medium/720_longest_word_in_dictionary.rb) | [Link](./test/medium/test_720_longest_word_in_dictionary.rb) |
| 725. Split Linked List in Parts | [Link](https://leetcode.com/problems/split-linked-list-in-parts/) | [Link](./lib/medium/725_split_linked_list_in_parts.rb) | [Link](./test/medium/test_725_split_linked_list_in_parts.rb) |
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 = '7.5.0'
s.version = '7.5.1'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
1 change: 1 addition & 0 deletions lib/common/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def self.from_array(values)
# @return {Boolean}
def self.are_equals(first, second)
return true if first.nil? && second.nil?

return false if first.nil? || second.nil?

first.val == second.val && are_equals(first.next, second.next)
Expand Down
38 changes: 38 additions & 0 deletions lib/medium/725_split_linked_list_in_parts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require_relative '../common/linked_list'

# https://leetcode.com/problems/split-linked-list-in-parts/
# @param {ListNode} head
# @param {Integer} k
# @return {ListNode[]}
def split_list_to_parts(head, k)
p = head
count = 0

until p.nil?
p = p.next
count += 1
end

part = count / k
remainder = count % k
result = []
p = head

(0...k).each do |i|
unit = ::ListNode.new(0)
curr = unit

(0...(part + (i < remainder ? 1 : 0))).each do |_|
next unless p

curr = curr.next = ::ListNode.new(p.val)
p = p.next
end

result << unit.next
end

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

require_relative '../test_helper'
require_relative '../../lib/common/linked_list'
require_relative '../../lib/medium/725_split_linked_list_in_parts'
require 'minitest/autorun'

class SplitLinkedListInPartsTest < ::Minitest::Test
def test_default_one
result = split_list_to_parts(
::ListNode.from_array(
[1, 2, 3]
),
5
)

assert_equal(5, result.size)

[
::ListNode.from_array([1]),
::ListNode.from_array([2]),
::ListNode.from_array([3]),
::ListNode.from_array([]),
::ListNode.from_array([])
].each_with_index do |list, index|
assert(
::ListNode.are_equals(
list,
result[index]
)
)
end
end

def test_default_two
result = split_list_to_parts(
::ListNode.from_array(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
),
3
)

assert_equal(3, result.size)

[
::ListNode.from_array(
[1, 2, 3, 4]
),
::ListNode.from_array(
[5, 6, 7]
),
::ListNode.from_array(
[8, 9, 10]
)
].each_with_index do |list, index|
assert(
::ListNode.are_equals(
list,
result[index]
)
)
end
end
end
Loading