-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path599. Minimum Index Sum of Two Lists.py
More file actions
51 lines (41 loc) · 1.93 KB
/
599. Minimum Index Sum of Two Lists.py
File metadata and controls
51 lines (41 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
https://leetcode.com/problems/minimum-index-sum-of-two-lists/
Given two arrays of strings list1 and list2, find the common strings with the least index sum.
A common string is a string that appeared in both list1 and list2.
A common string with the least index sum is a common string such that if it appeared at list1[i] and list2[j]
then i + j should be the minimum value among all the other common strings.
Return all the common strings with the least index sum. Return the answer in any order.
Example 1:
Input: list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["Piatti","The Grill at Torrey Pines",
"Hungry Hunter Steakhouse","Shogun"]
Output: ["Shogun"]
Explanation: The only common string is "Shogun".
Example 2:
Input: list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["KFC","Shogun","Burger King"]
Output: ["Shogun"]
Explanation: The common string with the least index sum is "Shogun" with index sum = (0 + 1) = 1.
Example 3:
Input: list1 = ["happy","sad","good"], list2 = ["sad","happy","good"]
Output: ["sad","happy"]
Explanation: There are three common strings:
"happy" with index sum = (0 + 1) = 1.
"sad" with index sum = (1 + 0) = 1.
"good" with index sum = (2 + 2) = 4.
The strings with the least index sum are "sad" and "happy".
"""
from typing import List
class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
positions = {item: idx for idx, item in enumerate(list1)}
least_sum = 2000
result = []
for list2_pos, item in enumerate(list2):
if item in positions:
list1_pos = positions[item]
cur_sum = list1_pos + list2_pos
if cur_sum < least_sum:
least_sum = cur_sum
result = [item]
elif cur_sum == least_sum:
result.append(item)
return result