Short Python


  • 0

    This one is very straight forward. We maintain dic1 and dic2 to make our look up can be achieved in average O(1) time and build a dictionary common to store the common interests. Then just return the keys with MIN values.

    class Solution(object):
        def findRestaurant(self, list1, list2):
            """
            :type list1: List[str]
            :type list2: List[str]
            :rtype: List[str]
            """
            dic1, dic2 = {x:i for i, x in enumerate(list1)}, {x:i for i, x in enumerate(list2)}
            common = {key: dic1[key] + dic2[key] for key in dic1 if key in dic2}
            MIN = min(common.values())
            return [key for key in common if common[key] == MIN]
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.