diff --git a/README.md b/README.md index b684f2ad..04062619 100644 --- a/README.md +++ b/README.md @@ -642,6 +642,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 916. Word Subsets | [Link](https://leetcode.com/problems/word-subsets/) | [Link](./lib/medium/916_word_subsets.rb) | [Link](./test/medium/test_916_word_subsets.rb) | | 919. Complete Binary Tree Inserter | [Link](https://leetcode.com/problems/complete-binary-tree-inserter/) | [Link](./lib/medium/919_complete_binary_tree_inserter.rb) | [Link](./test/medium/test_919_complete_binary_tree_inserter.rb) | | 921. Minimum Add to Make Parentheses Valid | [Link](https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/) | [Link](./lib/medium/921_minimum_add_to_make_parentheses_valid.rb) | [Link](./test/medium/test_921_minimum_add_to_make_parentheses_valid.rb) | +| 937. Reorder Data in Log Files | [Link](https://leetcode.com/problems/reorder-data-in-log-files/) | [Link](./lib/medium/937_reorder_data_in_log_files.rb) | [Link](./test/medium/test_937_reorder_data_in_log_files.rb) | | 1400. Construct K Palindrome Strings | [Link](https://leetcode.com/problems/construct-k-palindrome-strings/) | [Link](./lib/medium/1400_construct_k_palindrome_strings.rb) | [Link](./test/medium/test_1400_construct_k_palindrome_strings.rb) | | 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) | | 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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 8f4ecca3..ec48b6fb 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.9.9' + s.version = '8.0.0' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/937_reorder_data_in_log_files.rb b/lib/medium/937_reorder_data_in_log_files.rb new file mode 100644 index 00000000..057833c7 --- /dev/null +++ b/lib/medium/937_reorder_data_in_log_files.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/reorder-data-in-log-files/ +# @param {String[]} logs +# @return {String[]} +def reorder_log_files(logs) + partitions = + logs.partition do |log| + log = log.split + !('0'..'9').include?(log[1][0]) + end + + partitions[0].sort! do |p1, p2| + p1_split = p1.split + p2_split = p2.split + + if p1_split[1..] == p2_split[1..] + p1_split[0] <=> p2_split[0] + else + p1_split[1..] <=> p2_split[1..] + end + end + + partitions.flatten +end diff --git a/test/medium/test_937_reorder_data_in_log_files.rb b/test/medium/test_937_reorder_data_in_log_files.rb new file mode 100644 index 00000000..06eebfcd --- /dev/null +++ b/test/medium/test_937_reorder_data_in_log_files.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/937_reorder_data_in_log_files' +require 'minitest/autorun' + +class ReorderDataInLogFilesTest < ::Minitest::Test + def test_default_one + assert_equal( + [ + 'let1 art can', + 'let3 art zero', + 'let2 own kit dig', + 'dig1 8 1 5 1', + 'dig2 3 6' + ], + reorder_log_files( + [ + 'dig1 8 1 5 1', + 'let1 art can', + 'dig2 3 6', + 'let2 own kit dig', + 'let3 art zero' + ] + ) + ) + end + + def test_default_two + assert_equal( + [ + 'g1 act car', + 'a8 act zoo', + 'ab1 off key dog', + 'a1 9 2 3 1', + 'zo4 4 7' + ], + reorder_log_files( + [ + 'a1 9 2 3 1', + 'g1 act car', + 'zo4 4 7', + 'ab1 off key dog', + 'a8 act zoo' + ] + ) + ) + end + + def test_additional_one + assert_equal( + [ + 'a2 act car', + 'g1 act car', + 'a8 act zoo', + 'ab1 off key dog', + 'a1 9 2 3 1', + 'zo4 4 7' + ], + reorder_log_files( + [ + 'a1 9 2 3 1', + 'g1 act car', + 'zo4 4 7', + 'ab1 off key dog', + 'a8 act zoo', + 'a2 act car' + ] + ) + ) + end +end