Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. class TimeMap {
  2. public:
  3.     /** Initialize your data structure here. */
  4.     std::unordered_map<std::string,std::map<int,string>> timeMap;
  5.     TimeMap() {
  6.        
  7.     }
  8.    
  9.     void set(string key, string value, int timestamp) {
  10.         timeMap[key][timestamp]=value;
  11.     }
  12.    
  13.     string get(string key, int timestamp) {
  14.         if (timeMap.find(key)==timeMap.end())
  15.             return "";
  16.         auto it = std::lower_bound(timeMap[key].begin(), timeMap[key].end(), timestamp, [](const auto& a, const auto& b)->bool{return a.first<b;});
  17.         if (it==timeMap[key].begin())
  18.             return "";
  19.         if (it==timeMap[key].end())
  20.             it--;
  21.         return it->second;
  22.     }
  23. };
  24.  
  25. /**
  26.  * Your TimeMap object will be instantiated and called as such:
  27.  * TimeMap* obj = new TimeMap();
  28.  * obj->set(key,value,timestamp);
  29.  * string param_2 = obj->get(key,timestamp);
  30.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement