75-days-dsa-challenge

Ninja technique🥷 to ACE DSA Interviews.

View the Project on GitHub

981. Time Based Key-Value Store

Binary search and map

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;
    }
};