Minimum Index Sum of Two Lists
LeetCode: Minimum Index Sum of Two Lists
Problem:
Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings.
You need to help them find out their common interest with the least list index sum.
If there is a choice tie between answers, output all of them with no order requirement. You could assume there always exists an answer.
- Note:
1. The length of both lists will be in the range of [1, 1000].
2. The length of strings in both lists will be in the range of [1, 30].
3. The index is starting from 0 to the list length minus 1.
4. No duplicates in both lists.
- Examples:
Input:
["Shogun", "Tapioca Express", "Burger King", "KFC"]
["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"]
Output: ["Shogun"]
Explanation: The only restaurant they both like is "Shogun".
Input:
["Shogun", "Tapioca Express", "Burger King", "KFC"]
["KFC", "Shogun", "Burger King"]
Output: ["Shogun"]
Explanation: The restaurant they both like and have the least index sum is "Shogun" with index sum 1 (0+1).
- Analysis:
Use a hashmap to count the index of one of the input lists.
Use the hashmap to get the minimal index sum of the common string of both strings of lists.
- Code - Java:
class Solution {
public String[] findRestaurant(String[] list1, String[] list2) {
Map<String, Integer> indexes = new HashMap<String, Integer>();
int len1 = list1.length;
int len2 = list2.length;
for (int i = 0; i < len1; i++) {
indexes.put(list1[i], i);
}
List<String> list = new ArrayList<String>();
int sum = len1 + len2 - 2;
for (int i = 0; i < len2; i++) {
int count = indexes.getOrDefault(list2[i], -1);
if (count == -1) {
continue;
}
int temp = count + i;
if (temp < sum) {
list.clear();
sum = temp;
list.add(list2[i]);
} else if (temp == sum) {
list.add(list2[i]);
}
}
int size = list.size();
String[] results = new String[size];
for (int i = 0; i < size; i++) {
results[i] = list.get(i);
}
return results;
}
}
- Code - C++:
class Solution {
public:
vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
map<string, int> indexes;
int len1 = list1.size();
int len2 = list2.size();
for (int i = 0; i < len1; i++) {
indexes.insert(pair<string, int>(list1[i], i));
}
vector<string> list;
int sum = len1 + len2 - 2;
for (int i = 0; i < len2; i++) {
if (indexes.find(list2[i]) != indexes.end()) {
int temp = indexes[list2[i]] + i;
if (temp < sum) {
list.clear();
sum = temp;
list.push_back(list2[i]);
} else if (temp == sum) {
list.push_back(list2[i]);
}
}
}
return list;
}
};
- Code - Python:
class Solution(object):
def findRestaurant(self, list1, list2):
"""
:type list1: List[str]
:type list2: List[str]
:rtype: List[str]
"""
indexes = {}
len1, len2 = len(list1), len(list2)
for i in range(len1):
indexes[list1[i]] = i
result = []
minVal = len1 + len2 - 2
for i in range(len2):
if list2[i] in indexes:
temp = indexes[list2[i]] + i
if temp < minVal:
del result[:]
minVal = temp
result.append(list2[i])
elif temp == minVal:
result.append(list2[i])
return result
Time Complexity: O(N + M), N is the size of one of the input list as A, M is the other as B.
Space Complexity: O(N), N is the size of the input list as A