[599. Minimum Index Sum of Two Lists] c++_Ac


  • 0
    Solution {
     public:
     vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) 
    {
        vector<string> res;
        unordered_map<string, int> mp1;
        unordered_map<string, int> mp2;
        for(int i = 0; i < list1.size(); ++i){
            mp1[list1[i]] = i;
        }
        for(int i = 0; i < list2.size(); ++i){
            mp2[list2[i]] = i;
        }
        int Index = INT_MAX;
        for(auto m: mp1){
            if(mp2.find(m.first) != mp2.end()){
                int tmp = mp2[m.first] + m.second;
                if(tmp == Index){
                    res.push_back(m.first);
                }else if(tmp < Index){
                    Index = tmp;
                    res.clear();
                    res.push_back(m.first);
                }
                mp2.erase(m.first);
            }
        }
        return res;
    }
    };

Log in to reply
 

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