diff --git a/README.md b/README.md index 118482ac..ac343fad 100644 --- a/README.md +++ b/README.md @@ -126,3 +126,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 589. N-ary Tree Preorder Traversal | [Link](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [Link](./lib/easy/589_n_ary_tree_preorder_traversal.rb) | | 590. N-ary Tree Postorder Traversal | [Link](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [Link](./lib/easy/590_n_ary_tree_postorder_traversal.rb) | | 594. Longest Harmonious Subsequence | [Link](https://leetcode.com/problems/longest-harmonious-subsequence/) | [Link](./lib/easy/594_longest_harmonious_subsequence.rb) | +| 599. Minimum Index Sum of Two Lists | [Link](https://leetcode.com/problems/minimum-index-sum-of-two-lists/) | [Link](./lib/easy/599_minimum_index_sum_of_two_lists.rb) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 540dc94c..e3453904 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 = '1.2.0' + s.version = '1.2.1' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE] s.executable = 'leetcode-ruby' diff --git a/lib/easy/599_minimum_index_sum_of_two_lists.rb b/lib/easy/599_minimum_index_sum_of_two_lists.rb new file mode 100644 index 00000000..96ef9733 --- /dev/null +++ b/lib/easy/599_minimum_index_sum_of_two_lists.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/minimum-index-sum-of-two-lists/ +# @param {String[]} list1 +# @param {String[]} list2 +# @return {String[]} +def find_restaurant(list1, list2) + list1_map = {} + list1.each_with_index { |val, index| list1_map[val] = index } + min = 1_000_000 + result = [] + list2.each_with_index do |val, index| + next unless list1_map.include?(val) + + sum = index + list1_map[val] + next unless sum <= min + + result.clear if sum != min + min = sum + result << val + end + + result +end diff --git a/test/easy/test_599_minimum_index_sum_of_two_lists.rb b/test/easy/test_599_minimum_index_sum_of_two_lists.rb new file mode 100644 index 00000000..b1089405 --- /dev/null +++ b/test/easy/test_599_minimum_index_sum_of_two_lists.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/easy/599_minimum_index_sum_of_two_lists' +require 'minitest/autorun' + +class MinimumIndexSumOfTwoListsTest < ::Minitest::Test + def test_default + assert_equal( + ['Shogun'], + find_restaurant( + ['Shogun', 'Tapioca Express', 'Burger King', 'KFC'], + ['Piatti', 'The Grill at Torrey Pines', 'Hungry Hunter Steakhouse', 'Shogun'] + ) + ) + assert_equal( + ['Shogun'], + find_restaurant( + ['Shogun', 'Tapioca Express', 'Burger King', 'KFC'], + ['KFC', 'Shogun', 'Burger King'] + ) + ) + assert_equal( + %w[sad happy], + find_restaurant( + %w[happy sad good], + %w[sad happy good] + ) + ) + end +end