标签:ril this help tables nat func tween ioc ble
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:
Solution one: hash table + heap
This is the second problem of leetcode weekly contest 34. We need data structures to solve it. We should return the minimal index sum for common interests. There is two work for us to do.
We should do some change on the heapsort function to return the minimal index sum
Time complexity:
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
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]; } };
599. Minimum Index Sum of Two Lists
标签:ril this help tables nat func tween ioc ble
原文地址:http://www.cnblogs.com/wdw828/p/6917194.html