struct valueData {
string value;
int timestamp;
valueData(string value, int timestamp) : value(value), timestamp(timestamp) {}
};
class TimeMap {
public:
TimeMap() {
}
void set(string key, string value, int timestamp) {
htbl[key].push_back(valueData(value, timestamp));
}
string get(string key, int timestamp) {
auto iter = htbl.find(key);
if (iter == htbl.end()) {
return "";
}
vector<valueData> &valueList = iter->second;
if (timestamp < valueList[0].timestamp) {
return "";
}
if (valueList.back().timestamp < timestamp) {
return valueList.back().value;
}
int left = 0, right = valueList.size() - 1;
while (left < right) {
int middle = left + (right - left + 1) / 2;
if (timestamp < valueList[middle].timestamp) {
right = middle - 1;
} else {
left = middle;
}
}
if (timestamp < valueList[left].timestamp) {
--left;
}
return valueList[left].value;
}
private:
unordered_map<string, vector<valueData>> htbl;
};
/**
* 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);
*/
TimeMap
set
get
key
.TimeMap
set
key
.key
.get
My Solution Solution 1: DFS (recursion) The Key Idea for Solving This Coding Question C++ Code /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right;
Jun 6, 2025MyCircularQueueO(k)
Apr 20, 2025O(m)
Mar 4, 2025O(n)n is the length of nums.
Feb 19, 2025or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up