-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from fartem/599_Minimum_Index_Sum_of_Two_Lists
2023-01-16 v. 1.2.1: added "599. Minimum Index Sum of Two Lists"
- Loading branch information
Showing
4 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |