leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0981.cpp (737B)


0 class TimeMap { 1 unordered_map<string, vector<pair<int, string>>> um; 2 3 public: 4 void set(const string &key, const string &value, int timestamp) { 5 um[key].emplace_back(timestamp, value); 6 } 7 8 string get(const string &key, int timestamp) { 9 const auto &vec = um[key]; 10 int low = 0, high = size(vec) - 1; 11 string res = ""; 12 13 while (low <= high) { 14 const auto mid = low + (high - low) / 2; 15 16 if (vec[mid].first == timestamp) return vec[mid].second; 17 if (vec[mid].first >= timestamp) 18 high = mid - 1; 19 else { 20 res = vec[mid].second; 21 low = mid + 1; 22 } 23 } 24 25 return res; 26 } 27 };