Created
January 31, 2019 12:25
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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