-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path599.cpp
More file actions
23 lines (22 loc) · 738 Bytes
/
599.cpp
File metadata and controls
23 lines (22 loc) · 738 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
unordered_map<string, int> mp;
for (int i = 0; i < list1.size(); i++) mp[list1[i]] = i;
vector<string> res;
int minVal = INT_MAX;
for (int i = 0; i < list2.size(); i++) {
if (mp.find(list2[i]) != mp.end()) {
if (i + mp[list2[i]] < minVal) {
minVal = i + mp[list2[i]];
res.clear();
res.push_back(list2[i]);
}
else if (i + mp[list2[i]] == minVal) {
res.push_back(list2[i]);
}
}
}
return res;
}
};