diff --git a/README.md b/README.md index 234a9302..72baa8c1 100644 --- a/README.md +++ b/README.md @@ -624,3 +624,9 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 797. All Paths From Source to Target | [Link](https://leetcode.com/problems/all-paths-from-source-to-target/) | [Link](./lib/medium/797_all_paths_from_source_to_target.rb) | [Link](./test/medium/test_797_all_paths_from_source_to_target.rb) | | 814. Binary Tree Pruning | [Link](https://leetcode.com/problems/binary-tree-pruning/) | [Link](./lib/medium/814_binary_tree_pruning.rb) | [Link](./test/medium/test_814_binary_tree_pruning.rb) | | 817. Linked List Components | [Link](https://leetcode.com/problems/linked-list-components/) | [Link](./lib/medium/817_linked_list_components.rb) | [Link](./test/medium/test_817_linked_list_components.rb) | + +### Hard + +| Name | Link to LeetCode | Link to solution | Link to tests | +| ------------------------------ | ------------------------------------------------------------------ | --------------------------------------------------- | --------------------------------------------------------- | +| 4. Median of Two Sorted Arrays | [Link](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [Link](./lib/hard/4_median_of_two_sorted_arrays.rb) | [Link](./test/hard/test_4_median_of_two_sorted_arrays.rb) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 3c1a6c85..01cc3213 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.9' + s.version = '7.6.0' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/hard/4_median_of_two_sorted_arrays.rb b/lib/hard/4_median_of_two_sorted_arrays.rb new file mode 100644 index 00000000..03c4dea7 --- /dev/null +++ b/lib/hard/4_median_of_two_sorted_arrays.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/median-of-two-sorted-arrays/ +# @param {Integer[]} nums1 +# @param {Integer[]} nums2 +# @return {Float} +def find_median_sorted_arrays(nums1, nums2) + c = nums1 << nums2 + c.flatten!.sort! + + if c.size.even? + (c[c.size / 2] + c[c.size / 2 - 1]) / 2.0 + else + c[c.size / 2] + end +end diff --git a/test/hard/test_4_median_of_two_sorted_arrays.rb b/test/hard/test_4_median_of_two_sorted_arrays.rb new file mode 100644 index 00000000..e93015ef --- /dev/null +++ b/test/hard/test_4_median_of_two_sorted_arrays.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/hard/4_median_of_two_sorted_arrays' +require 'minitest/autorun' + +class MedianOfTwoSortedArraysTest < ::Minitest::Test + def test_default_one + assert_equal( + 2.00000, + find_median_sorted_arrays( + [1, 3], + [2] + ) + ) + end + + def test_default_two + assert_equal( + 2.50000, + find_median_sorted_arrays( + [1, 2], + [3, 4] + ) + ) + end +end