C++ Used maps and sets together


#1
vector<int> Solution::solve(vector<int> &A) {
    map<int,set<int>> mp;
    int n = A.size();
    vector<int> ans;
    int j= 0;
    for(int i = 0; i<n; i++){
        map<int,set<int>>::iterator itr = mp.find(A[i]);
        if(itr==mp.end()){
            mp[A[i]].insert(i);
            ans.push_back(A[i]);
            continue;
        }
        set<int> st = itr->second;
        if(st.size()){
            int index = *st.begin();
            ans[index]++;
            st.erase(st.begin());
            st.insert(i);
            mp[A[i]] = st;
            mp[ans[index]].insert(index);
            ans.push_back(A[i]);
            continue;
        }
        ans.push_back(A[i]);
        mp[A[i]].insert(i);
        
    }
    return ans;
}