Skip to content

Instantly share code, notes, and snippets.

@fpdjsns
Created January 31, 2019 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fpdjsns/359ef8a3b83792c2738295eb70fe6170 to your computer and use it in GitHub Desktop.
Save fpdjsns/359ef8a3b83792c2738295eb70fe6170 to your computer and use it in GitHub Desktop.
[leetcode] 981. Time Based Key-Value Store : https://leetcode.com/problems/time-based-key-value-store/
class TimeMap {
private:
unordered_map<string, map<int, string>> m; // {key, {timestamp, value}}
public:
/** Initialize your data structure here. */
TimeMap() {
}
void set(string key, string value, int timestamp) {
m[key][timestamp] = value;
}
string get(string key, int timestamp) {
unordered_map<string, map<int,string>>:: iterator f = m.find(key);
if(f == m.end())
return "";
map<int, string>::iterator ans = (f->second).upper_bound(timestamp);
if(ans == (f->second).begin())
return "";
else
return prev(ans)->second;
}
};
/**
* 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);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment