Data-Structures-and-Algorithms icon indicating copy to clipboard operation
Data-Structures-and-Algorithms copied to clipboard

Leetcode 981. Time Based Key-Value Store

Open NarpatAanjana opened this issue 2 years ago • 0 comments

class TimeMap { public: unordered_map<string, set<pair<int,string>>> mp; TimeMap() { mp.clear(); }

void set(string key, string value, int timestamp) {
    mp[key].insert({timestamp, value}); 
}

string get(string key, int timestamp) {
    if(mp[key].size() != 0){
        auto it = mp[key].upper_bound({timestamp, "~"});
        if(it == mp[key].begin()) return "";
        --it;
        return it->second;
    }
    return "";
}

};

NarpatAanjana avatar Oct 06 '22 16:10 NarpatAanjana