LeetCode #599 Minimum Index Sum of Two Lists 两个列表的最小索引总和

599 Minimum Index Sum of Two Lists 两个列表的最小索引总和

Description:
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:

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:
The length of both lists will be in the range of [1, 1000].
The length of strings in both lists will be in the range of [1, 30].
The index is starting from 0 to the list length minus 1.
No duplicates in both lists.

题目描述:
假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示。

你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅。 如果答案不止一个,则输出所有答案并且不考虑顺序。 你可以假设总是存在一个答案。

示例 :

示例 1:

输入:
["Shogun", "Tapioca Express", "Burger King", "KFC"]
["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"]
输出: ["Shogun"]
解释: 他们唯一共同喜爱的餐厅是“Shogun”。

示例 2:

输入:
["Shogun", "Tapioca Express", "Burger King", "KFC"]
["KFC", "Shogun", "Burger King"]
输出: ["Shogun"]
解释: 他们共同喜爱且具有最小索引和的餐厅是“Shogun”,它有最小的索引和1(0+1)。

提示:

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

思路:

用两个 map, 第一个 map记录 list1中出现的元素及下标, 第二个 map记录 list2中在 list1中已经出现的元素及下标和, 遍历第二个 map记录最小元素和和元素即可
时间复杂度O(n), 空间复杂度O(n)

代码:
C++:

class Solution 
{
public:
    vector findRestaurant(vector& list1, vector& list2) 
    {
        unordered_map m1;
        unordered_map m2;
        vector result;
        for (int i = 0; i < list1.size(); i++) m1[list1[i]] = i;
        for (int i = 0; i < list2.size(); i++) 
        {
            if (m1.count(list2[i])) m2[list2[i]] = m1[list2[i]] + i;
        }
        int temp = 2000;
        for (auto m = m2.begin(); m != m2.end(); m++) 
        {
            if (temp > m -> second) 
            {
                result.clear();
                result.push_back(m -> first);
                temp = m -> second;
            } 
            else if (temp == m -> second) result.push_back(m -> first);
        }
        return result;
    }
};

Java:

class Solution {
    public String[] findRestaurant(String[] list1, String[] list2) {
        Map map = new HashMap<>(list1.length);
        for (int i = 0; i < list1.length; i++) map.put(list1[i], i);
        List result = new ArrayList<>();
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < list2.length; i++) {
            if (map.containsKey(list2[i])) {
                int j = map.get(list2[i]);
                if (i + j < min) {
                    result.clear();
                    result.add(list2[i]);
                    min = i + j;
                } else if (i + j == min) result.add(list2[i]);
            }
        }
        return result.toArray(new String[result.size()]);
    }
}

Python:

class Solution:
    def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
        d = {x: list1.index(x) + list2.index(x) for x in set(list1) & set(list2)}
        return [x for x in d if d[x] == min(d.values())]

你可能感兴趣的:(LeetCode #599 Minimum Index Sum of Two Lists 两个列表的最小索引总和)