Leetcode 599 Solution

This article provides solution to leetcode question 599 (minimum-index-sum-of-two-lists)

https://leetcode.com/problems/minimum-index-sum-of-two-lists

Solution

class Solution:
    def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
        m = collections.defaultdict(list)

        for i, rest in enumerate(list1):
            m[rest].append(i)

        for i, rest in enumerate(list2):
            m[rest].append(i)

        max_c = 1000000
        ans = []
        for rest, indexes in m.items():
            if len(indexes) < 2:
                continue

            if max_c > sum(indexes):
                max_c = sum(indexes)
                ans = [rest]
            elif max_c == sum(indexes):
                ans.append(rest)

        return ans