zoukankan      html  css  js  c++  java
  • 599. Minimum Index Sum of Two Lists

    Problem statement:

    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.

    Solution one: hash table + heap

    This is the second problem of leetcode weekly contest 34. We need data structures to solve it. There is two work for us to do:

    • Find the common interests ---> hash table(unordered_map)
    • Find the minimal index sum among all common interests  ---> heap(priority_queue).

    We should redefine the heapsort function(lambda function) to return the minimal index sum

    Time complexity: 

    • Loop for this two string lists: O(l1 + l2)
    • Loop for the heap to find the minimal index sum. Since the time complexity to reheapify after one element is popped from the heap is O(klgk). k is the number of common interests.

    Time complexity is O(l1 + l2) + O(klgk). 

    Space complexity is O(l1 + k).

    class Solution {
    public:
        vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
            auto comp = [](pair<int, string>& left, pair<int, string>& right){ return left.first > right.first; };
            priority_queue<pair<int, string>, vector<pair<int, string>>, decltype(comp)> pq(comp);
            unordered_map<string, int> hash_table; // <"restaurant", index_sum>
            for(int i = 0; i < list1.size(); i++){
                hash_table[list1[i]] = i; 
            }
            for(int i = 0; i < list2.size(); i++){
                if(hash_table.count(list2[i])){
                    pq.push({hash_table[list2[i]] + i, list2[i]});
                }
            }
            vector<string> common_interests;
            int min_idx = pq.top().first;
            while(!pq.empty()){
                if(pq.top().first == min_idx){
                    common_interests.push_back(pq.top().second);
                    pq.pop();
                } else {
                    break;
                }
            }
            return common_interests;
        }
    };

    Solution two: two hash tables

    • Loop the first list and insert each element into the hash table index by the name of a restaurant.
    • Loop the second list, if the current restaurant is a common interest and the index sum of them is:
      • less than the current value, create a new item in the second hash table indexed by the index sum.
      • equal to current value, push back to the solution.

    Time complexity is O(l1 + l2).

    Space complexity is O(l1 + k).

    class Solution {
    public:
        vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
            unordered_map<string, int> hash_table; // <string, index>
            for(int i = 0; i < list1.size(); i++){
                hash_table[list1[i]] = i;
            }
            unordered_map<int, vector<string>> common_interests;
            int min_common = INT_MAX;
            string restaurant_name;
            for(int j = 0; j < list2.size(); j++){
                if(hash_table.count(list2[j])){
                    if(hash_table[list2[j]] + j <= min_common){
                        min_common = hash_table[list2[j]] + j;
                        restaurant_name = list2[j];
                        common_interests[min_common].push_back(restaurant_name);
                    }
                }
            }
            return common_interests[min_common];
        }
    };
  • 相关阅读:
    git命令_保存本地变更+拉取+合并代码+推送代码到远程仓+添加CI触发器变量
    debian_linux_apt-get命令_dpkg命令
    debian_linux系统_访问真实环境rancher_证书问题相关_https相关_使用kubectl命令行查看资源时报错:Unable to connect to the server: x509: certificate signed by unknown authority
    linux_xargs入门介绍_及和for循环命令区别
    技术实践丨GaussDB(DWS)运维管理功能“升级”的原理和使用
    华为云数据安全中心正式公测,8大核心数据安全能力守护你的数据
    华为鲲鹏专家解读:90%代码如何移植到鲲鹏平台
    数据库技术丨GaussDB(DWS)数据同步状态查看方法
    你掉进过“伪敏捷”的陷阱吗?
    拯救深度学习:标注数据不足下的深度学习方法
  • 原文地址:https://www.cnblogs.com/wdw828/p/6917194.html
Copyright © 2011-2022 走看看