C++ straightforward solution


  • 0
    Q
    class Solution {
    public:
        vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
            map<string,int> mp;
            vector<string> res;
            int m = INT_MAX;
            for(int i=0;i<list1.size();i++)
                mp[list1[i]] = i;
            
            for(int i=0;i<list2.size();i++){
                if(mp.find(list2[i]) != mp.end()){
                    m = min(m,mp[list2[i]] + i);
                }
            }
            for(int i=0;i<list2.size();i++){
                if(mp.find(list2[i]) != mp.end()){
                    if(mp[list2[i]] + i == m)
                        res.push_back(list2[i]);
                }
            }
            return res;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.