diff --git a/README.md b/README.md index faecd003..c02d930e 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index d8504640..ad8f5d6a 100644 --- a/leetcode-ruby.gemspec +++ b/leetcode-ruby.gemspec @@ -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' diff --git a/lib/common/linked_list.rb b/lib/common/linked_list.rb index 8340bbed..c94fdb82 100644 --- a/lib/common/linked_list.rb +++ b/lib/common/linked_list.rb @@ -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) diff --git a/lib/medium/725_split_linked_list_in_parts.rb b/lib/medium/725_split_linked_list_in_parts.rb new file mode 100644 index 00000000..56a2bce5 --- /dev/null +++ b/lib/medium/725_split_linked_list_in_parts.rb @@ -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 diff --git a/test/medium/test_725_split_linked_list_in_parts.rb b/test/medium/test_725_split_linked_list_in_parts.rb new file mode 100644 index 00000000..f521f668 --- /dev/null +++ b/test/medium/test_725_split_linked_list_in_parts.rb @@ -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