Day1 -- Minimum Index Sum of Two Lists

From: https://leetcode.com/

mode: random pick

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.

My solution:

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 # Time    : 2018/11/14 
 4 
 5 
 6 class Solution:
 7 
 8     def _fistQuery(self, list1, list2):
 9         if len(list1) == 1:
10             return list1
11         if len(list2) == 1:
12             return list2
13 
14     def findRestaurant(self, list1, list2):
15         """
16         :type list1: List[str]
17         :type list2: List[str]
18         :rtype: List[str]
19         """
20         fist_req = self._fistQuery(list1, list2)
21         if fist_req:
22             return fist_req
23         min_index = -1
24         temp_req = []
25         for index1, res in enumerate(list1):
26             if res in list2:
27                 index2 = list2.index(res)
28                 index_sum = index1+index2
29                 if min_index == -1:
30                     min_index = index_sum
31                     temp_req.append(res)
32                 else:
33                     if min_index == index_sum:
34                         temp_req.append(res)
35                     elif min_index > index1 + index2:
36                         temp_req = [res, ]
37         return temp_req
38 
39     def findRestaurant2(self, list1, list2):
40         """
41         :type list1: List[str]
42         :type list2: List[str]
43         :rtype: List[str]
44         """
45         fist_req = self._fistQuery(list1, list2)
46         if fist_req:
47             return fist_req
48         req_list = []
49         max_sum_index = len(list1) + len(list2) - 1
50         for i in range(max_sum_index):
51             j = 0
52             while i - j >= 0:
53                 if list1[j] == list2[i-j]:
54                     req_list.append(list1[j])
55                 j += 1
56             if req_list:
57                 return req_list
58 
59 
60 if __name__ == '__main__':
61     s = Solution()
62     print s.findRestaurant2(["Shogun", "Tapioca Express", "Burger King", "KFC"],
63                            ["KFC", "Shogun", "Burger King"])

总结:

这道题的难度是Easy,但是从中也是可以锻炼自己的思维能力。findRestaurant为第一种也是最容易想到的循环方式,findRestaurant2是根据结果的index sum来反向找element。另:经过大哲提醒set方式也是一种便捷思路

原文地址:https://www.cnblogs.com/khal-Cgg/p/9958012.html