Straightforward python solution with explanation


  • 0
    K

    Make a dictionary "andy_dict" of list1 with keys being restaurants, values being the indices. Go through list2 and check if restaurant in list2 is in andy_dict and add to result, with value being the sum the indices in list1 and list2 respectively.

    Finally go through result and find the minimum index (sum of indices). Output all the keys in result that have values matching the minimum index.

         def findRestaurant(self, list1, list2):
            result={}
            out=[]
            andy_dict = dict(zip(list1,range(len(list1))))
            for i,v in enumerate(list2):
                if v in andy_dict:
                    result.update({v:i+andy_dict[v]})
                    
            minIndex =len(list2) + len(list1)
            
            for k in result.keys():
                if result[k] < minIndex:
                    minIndex= result[k]
            
            for k in result.keys():
                if result[k] == minIndex:
                    out.append(k)
            return out
    

Log in to reply
 

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