Skip to content

Commit

Permalink
Merge pull request #170 from fartem/599_Minimum_Index_Sum_of_Two_Lists
Browse files Browse the repository at this point in the history
2023-01-16 v. 1.2.1: added "599. Minimum Index Sum of Two Lists"
  • Loading branch information
fartem authored Jul 16, 2023
2 parents 5f0cca1 + 8c08c41 commit 5fa2acb
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
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 = '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'
Expand Down
24 changes: 24 additions & 0 deletions lib/easy/599_minimum_index_sum_of_two_lists.rb
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions test/easy/test_599_minimum_index_sum_of_two_lists.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5fa2acb

Please sign in to comment.