[
string
hash table
]
Leetcode 0599 Minimum Index Sum of Two Lists
Problem statement
https://leetcode.com/problems/minimum-index-sum-of-two-lists/
Solution
Just put both lists to hash table and then traverse them, updating sum of indexes if needed.
Complexity
Time complexity is O((l1+l2) * x)
and space complexity is the same where l1
and l2
are numbers of restaurants in the first and in the second lists and x
is average length of name of restaurant.
Code
class Solution:
def findRestaurant(self, l1, l2):
d1 = {w: i for i, w in enumerate(l1)}
d2 = {w: i for i, w in enumerate(l2)}
min_sum = min(d1[x] + d2[x] for x in d1 if x in d2)
return [x for x in d1 if x in d2 and d1[x] + d2[x] == min_sum]