LeetCode Question: 981. Time Based Key-Value Store (Level : Medium)
Note:- Scroll horizontally to see the full line of code.
class TimeMap
{
map<string, map<int, string>> Dictionary;
public:
TimeMap()
{
}
void set(string key, string value, int timestamp)
{
Dictionary[key][timestamp] = value;
}
string get(string key, int timestamp)
{
if (Dictionary.find(key) == Dictionary.end())
{
return "";
}
else
{
if (Dictionary[key].find(timestamp) == Dictionary[key].end())
{
for (auto i = Dictionary[key].rbegin(); i != Dictionary[key].rend(); ++i)
{
if (i->first < timestamp)
{
return i->second;
}
}
return "";
}
else
{
return Dictionary[key][timestamp];
}
}
}
};
/**
* Your TimeMap object will be instantiated and called as such:
* TimeMap* obj = new TimeMap();
* obj->set(key,value,timestamp);
* string param_2 = obj->get(key,timestamp);
*/
Comments
Post a Comment