Effective C++ solution


#1
class TimeMap {
private:
    unordered_map<string, vector<int>> t;
    unordered_map<int, string> v;
    
public:
    void set(string key, string value, int timestamp) {
        t[key].emplace_back(timestamp);
        v[timestamp] = value;
    }
    
    string get(string key, int timestamp) {
        if(t.find(key) == end(t)) return "";
        auto ub = upper_bound(begin(t[key]), end(t[key]), timestamp);
        if(ub != begin(t[key])) return v[*(--ub)];
        return "";
    }
};

vector<string> Solution::solve(vector<int> &A, vector<string> &B, vector<string> &C, vector<int> &D) {
    vector<string> res;
    TimeMap m;
    for (int i=0; i<A.size(); i++) {
        string s;
        switch(A[i]) {
            case 1: m.set(B[i], C[i], D[i]); break;
            case 2: s = m.get(B[i], D[i]); break;
        }
        res.push_back(s=="" ? "null" : s);
    }
    return res;
}