Skip to content

Latest commit

 

History

History
29 lines (23 loc) · 579 Bytes

981_timeBasedKeyValueStore.md

File metadata and controls

29 lines (23 loc) · 579 Bytes

Binary search and map

  • The code is self explanatory.

Code

class TimeMap {
public:
    unordered_map<string, map<int, string>> mp;
    TimeMap() { }

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

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