{"id":2413,"date":"2018-04-02T23:11:40","date_gmt":"2018-04-03T06:11:40","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2413"},"modified":"2018-04-02T23:14:08","modified_gmt":"2018-04-03T06:14:08","slug":"leetcode-599-minimum-index-sum-of-two-lists","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-599-minimum-index-sum-of-two-lists\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 599. Minimum Index Sum of Two Lists"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings.<\/p>\n<p>You need to help them find out their\u00a0<b>common interest<\/b>\u00a0with the\u00a0<b>least list index sum<\/b>. If there is a choice tie between answers, output all of them with no order requirement. You could assume there always exists an answer.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b>\r\n[\"Shogun\", \"Tapioca Express\", \"Burger King\", \"KFC\"]\r\n[\"Piatti\", \"The Grill at Torrey Pines\", \"Hungry Hunter Steakhouse\", \"Shogun\"]\r\n<b>Output:<\/b> [\"Shogun\"]\r\n<b>Explanation:<\/b> The only restaurant they both like is \"Shogun\".\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"crayon:false \"><b>Input:<\/b>\r\n[\"Shogun\", \"Tapioca Express\", \"Burger King\", \"KFC\"]\r\n[\"KFC\", \"Shogun\", \"Burger King\"]\r\n<b>Output:<\/b> [\"Shogun\"]\r\n<b>Explanation:<\/b> The restaurant they both like and have the least index sum is \"Shogun\" with index sum 1 (0+1).\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>The length of both lists will be in the range of [1, 1000].<\/li>\n<li>The length of strings in both lists will be in the range of [1, 30].<\/li>\n<li>The index is starting from 0 to the list length minus 1.<\/li>\n<li>No duplicates in both lists.<\/li>\n<\/ol>\n<h1><strong>Solution<\/strong><\/h1>\n<p>Time complexity: O(n+m)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 103 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;string&gt; findRestaurant(vector&lt;string&gt;&amp; list1, vector&lt;string&gt;&amp; list2) {\r\n    unordered_map&lt;string, int&gt; indices;\r\n    for (int i = 0; i &lt; list1.size(); ++i)\r\n      indices[list1[i]] = i;\r\n    \r\n    vector&lt;string&gt; ans; \r\n    int best_index = INT_MAX;\r\n    string best;\r\n    \r\n    for (int i = 0; i &lt; list2.size(); ++i) {\r\n      if (!indices.count(list2[i])) continue;\r\n      int index = indices[list2[i]] + i;\r\n      if (index &lt; best_index) ans.clear(); \r\n      if (index &lt;= best_index) {\r\n        best_index = index;\r\n        ans.push_back(list2[i]);\r\n      }\r\n    }    \r\n    \r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings. You&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70,47],"tags":[222,82],"class_list":["post-2413","post","type-post","status-publish","format-standard","hentry","category-hashtable","category-string","tag-easy","tag-hashtable","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2413","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=2413"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2413\/revisions"}],"predecessor-version":[{"id":2415,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2413\/revisions\/2415"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}