nikunjsoni

981

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