0599. Minimum Index Sum of Two Lists

# 599. Minimum Index Sum of Two Lists#

## 题目 #

Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings.

You need to help them find out their common interest with the least list index sum. If there is a choice tie between answers, output all of them with no order requirement. You could assume there always exists an answer.

Example 1:

``````Input:
["Shogun", "Tapioca Express", "Burger King", "KFC"]
["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"]
Output: ["Shogun"]
Explanation: The only restaurant they both like is "Shogun".
``````

Example 2:

``````Input:
["Shogun", "Tapioca Express", "Burger King", "KFC"]
["KFC", "Shogun", "Burger King"]
Output: ["Shogun"]
Explanation: The restaurant they both like and have the least index sum is "Shogun" with index sum 1 (0+1).
``````

Note:

1. The length of both lists will be in the range of [1, 1000].
2. The length of strings in both lists will be in the range of [1, 30].
3. The index is starting from 0 to the list length minus 1.
4. No duplicates in both lists.

## 题目大意 #

• 两个列表的长度范围都在 [1, 1000] 内。
• 两个列表中的字符串的长度将在 [1，30] 的范围内。
• 下标从 0 开始，到列表的长度减 1。
• 两个列表都没有重复的元素。

## 解题思路 #

• 在 Andy 和 Doris 两人分别有各自的餐厅喜欢列表，要求找出两人公共喜欢的一家餐厅，如果共同喜欢的次数相同，都输出。这一题是简单题，用 map 统计频次，输出频次最多的餐厅。

## 代码 #

``````
package leetcode

func findRestaurant(list1 []string, list2 []string) []string {
m, ans := make(map[string]int, len(list1)), []string{}
for i, r := range list1 {
m[r] = i
}
for j, r := range list2 {
if _, ok := m[r]; ok {
m[r] += j
if len(ans) == 0 || m[r] == m[ans[0]] {
ans = append(ans, r)
} else if m[r] < m[ans[0]] {
ans = []string{r}
}
}
}
return ans
}

``````

Sep 6, 2020