|
| 1 | +""" https://leetcode.com/problems/minimum-index-sum-of-two-lists/ """ |
| 2 | + |
| 3 | +class Solution: |
| 4 | + def findRestaurant(self, list1, list2): |
| 5 | + commons = [] |
| 6 | + list1Indexes = [] |
| 7 | + list2Indexes = [] |
| 8 | + |
| 9 | + for str in list1: |
| 10 | + if str in list2: |
| 11 | + commons.append(str) |
| 12 | + list1Indexes.append(list1.index(str)) |
| 13 | + list2Indexes.append(list2.index(str)) |
| 14 | + |
| 15 | + if (len(commons) <= 1): |
| 16 | + return commons |
| 17 | + else: |
| 18 | + leastCommons = [] |
| 19 | + least = list1Indexes[0] + list2Indexes[0] |
| 20 | + leastCommons.append(commons[0]) |
| 21 | + |
| 22 | + for i in range(1, len(commons)): |
| 23 | + sum = list1Indexes[i] + list2Indexes[i] |
| 24 | + |
| 25 | + if (sum < least): |
| 26 | + least = sum |
| 27 | + leastCommons.clear() |
| 28 | + leastCommons.append(commons[i]) |
| 29 | + elif (sum == least): |
| 30 | + least = sum |
| 31 | + leastCommons.append(commons[i]) |
| 32 | + |
| 33 | + return leastCommons |
| 34 | + |
| 35 | +print(Solution().findRestaurant(["happy","sad","good"], ["sad","happy","good"])) |
0 commit comments