class Solution:
def findRestaurant(self, list1, list2):
index_map = {restaurant: idx for idx, restaurant in enumerate(list1)}
min_sum = float('inf')
result = []
for idx2, restaurant in enumerate(list2):
if restaurant in index_map:
idx1 = index_map[restaurant]
total_idx = idx1 + idx2
if total_idx < min_sum:
min_sum = total_idx
result = [restaurant]
elif total_idx == min_sum:
result.append(restaurant)
return result
class Solution {
public:
vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
unordered_map<string, int> index_map;
for (int i = 0; i < list1.size(); ++i) {
index_map[list1[i]] = i;
}
int min_sum = INT_MAX;
vector<string> result;
for (int j = 0; j < list2.size(); ++j) {
if (index_map.count(list2[j])) {
int total = index_map[list2[j]] + j;
if (total < min_sum) {
min_sum = total;
result.clear();
result.push_back(list2[j]);
} else if (total == min_sum) {
result.push_back(list2[j]);
}
}
}
return result;
}
};
class Solution {
public String[] findRestaurant(String[] list1, String[] list2) {
Map<String, Integer> indexMap = new HashMap<>();
for (int i = 0; i < list1.length; i++) {
indexMap.put(list1[i], i);
}
int minSum = Integer.MAX_VALUE;
List<String> result = new ArrayList<>();
for (int j = 0; j < list2.length; j++) {
if (indexMap.containsKey(list2[j])) {
int total = indexMap.get(list2[j]) + j;
if (total < minSum) {
minSum = total;
result.clear();
result.add(list2[j]);
} else if (total == minSum) {
result.add(list2[j]);
}
}
}
return result.toArray(new String[result.size()]);
}
}
var findRestaurant = function(list1, list2) {
const indexMap = {};
for (let i = 0; i < list1.length; i++) {
indexMap[list1[i]] = i;
}
let minSum = Number.MAX_SAFE_INTEGER;
let result = [];
for (let j = 0; j < list2.length; j++) {
if (indexMap.hasOwnProperty(list2[j])) {
let total = indexMap[list2[j]] + j;
if (total < minSum) {
minSum = total;
result = [list2[j]];
} else if (total === minSum) {
result.push(list2[j]);
}
}
}
return result;
};
list1
and list2
, representing favorite restaurants of two people, your task is to find all common restaurants with the least sum of their indices in both lists. If there are multiple answers with the same minimum index sum, return all of them in any order. Each restaurant name is unique within each list, and you should not reuse elements.
list1
with every restaurant in list2
, tracking the index sums for matches. However, this brute-force method is inefficient, especially for large lists, since it checks every possible pair.
To optimize, we can utilize a hash map (dictionary) to store the indices of restaurants in one list for quick lookup. This way, for each restaurant in the second list, we can instantly check if it's in the first list and calculate the index sum. By tracking the minimum sum found, we can build our result efficiently.
list1
and store each restaurant name as a key and its index as the value in a hash map.list2
:
list2
, check if it exists in the hash map (i.e., is also present in list1
).list1 = ["Shogun", "Tapioca Express", "Burger King", "KFC"]
list2 = ["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"]
list1
:
list2
:
list1
) + 3 (from list2
) = 3.
list1
and list2
. This is because you compare every pair of elements.list2
takes O(M), with constant-time lookups.list1
.