<p>Design a time-based key-value data structure that can store multiple values for the same key at different time stamps and retrieve the key&#39;s value at a certain timestamp.</p>

<p>Implement the <code>TimeMap</code> class:</p>

<ul>
	<li><code>TimeMap()</code> Initializes the object of the data structure.</li>
	<li><code>void set(String key, String value, int timestamp)</code> Stores the key <code>key</code> with the value <code>value</code> at the given time <code>timestamp</code>.</li>
	<li><code>String get(String key, int timestamp)</code> Returns a value such that <code>set</code> was called previously, with <code>timestamp_prev &lt;= timestamp</code>. If there are multiple such values, it returns the value associated with the largest <code>timestamp_prev</code>. If there are no values, it returns <code>&quot;&quot;</code>.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input</strong>
[&quot;TimeMap&quot;, &quot;set&quot;, &quot;get&quot;, &quot;get&quot;, &quot;set&quot;, &quot;get&quot;, &quot;get&quot;]
[[], [&quot;foo&quot;, &quot;bar&quot;, 1], [&quot;foo&quot;, 1], [&quot;foo&quot;, 3], [&quot;foo&quot;, &quot;bar2&quot;, 4], [&quot;foo&quot;, 4], [&quot;foo&quot;, 5]]
<strong>Output</strong>
[null, null, &quot;bar&quot;, &quot;bar&quot;, null, &quot;bar2&quot;, &quot;bar2&quot;]

<strong>Explanation</strong>
TimeMap timeMap = new TimeMap();
timeMap.set(&quot;foo&quot;, &quot;bar&quot;, 1);  // store the key &quot;foo&quot; and value &quot;bar&quot; along with timestamp = 1.
timeMap.get(&quot;foo&quot;, 1);         // return &quot;bar&quot;
timeMap.get(&quot;foo&quot;, 3);         // return &quot;bar&quot;, since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 is &quot;bar&quot;.
timeMap.set(&quot;foo&quot;, &quot;bar2&quot;, 4); // store the key &quot;foo&quot; and value &quot;bar2&quot; along with timestamp = 4.
timeMap.get(&quot;foo&quot;, 4);         // return &quot;bar2&quot;
timeMap.get(&quot;foo&quot;, 5);         // return &quot;bar2&quot;
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= key.length, value.length &lt;= 100</code></li>
	<li><code>key</code> and <code>value</code> consist of lowercase English letters and digits.</li>
	<li><code>1 &lt;= timestamp &lt;= 10<sup>7</sup></code></li>
	<li>All the timestamps <code>timestamp</code> of <code>set</code> are strictly increasing.</li>
	<li>At most <code>2 * 10<sup>5</sup></code> calls will be made to <code>set</code> and <code>get</code>.</li>
</ul>

class TimeMap {
public:
    // me - MLE
    unordered_map<string, vector<int>> keyToTime;
    unordered_map<string, unordered_map<int, string>> mmp;
    void set5(string key, string value, int timestamp) {
        keyToTime[key].push_back(timestamp);
        mmp[key][timestamp] = value;
    }

    string get5(string key, int timestamp) {
        auto arr = keyToTime[key];    
        auto it = upper_bound(arr.begin(), arr.end(), timestamp);

        if (it == arr.begin())  return "";

        it--;

        return mmp[key][*it];
    }

    // TLE
    // string to timestamps
    unordered_map<string, vector<int>> mp1;
    unordered_map<string, unordered_map<int, string>> keyVal;
    void set4(string key, string value, int timestamp) {
      mp1[key].push_back(timestamp);      
      keyVal[key][timestamp] = value;
    }

    string get4(string key, int timestamp) {
      auto v = mp1[key];
      auto it = upper_bound(v.begin(), v.end(), timestamp);
      return it == v.begin() ? "" : keyVal[key][*prev(it)];
    }

    // Solution1
    // set: O(logn)
    // get: O(logn)
    unordered_map<string, map<int, string>> m;
    void set3(string key, string value, int timestamp) {
      m[key].insert({ timestamp, value });
    }
    
    string get3(string key, int timestamp) {
      auto it = m[key].upper_bound(timestamp);
      return it == m[key].begin() ? "" : prev(it)->second;
    }
    
    // Solution2: Use "timestamps are only increasing"
    // set: O(1)
    // get: O(logn)
    unordered_map<string, vector<pair<int, string>>> mp;
    void set2(string key, string value, int timestamp) {
      mp[key].push_back({ timestamp, value });
    }
    
    string get2(string key, int timestamp) {
        // auto it = upper_bound(
        //     begin(mp[key]), 
        //     end(mp[key]), 
        //     pair<int, string>(timestamp, ""), 
        //     [](const pair<int, string>& a, const pair<int, string>& b) { return a.first < b.first; });
        
        auto it = upper_bound(begin(mp[key]), end(mp[key]), pair<int, string>(timestamp, ""), [](
        const pair<int, string>& a, const pair<int, string>& b) { return a.first < b.first; });

      return it == mp[key].begin() ? "" : prev(it)->second;
    }
    
    // Solution3:
    // unordered_map<string, vector<pair<int, string>>> mpp; // did not work
    unordered_map<string, vector<pair<string, int>>> mpp; // [key, vec[val, time]]
    void set(string key, string value, int timestamp) {
       mpp[key].push_back({value, timestamp});
    }
    
    string get(string key, int timestamp) {
        auto& v = mpp[key];
        auto it = upper_bound(v.begin(), v.end(), timestamp, [](int val, auto& p){
            return val < p.second;
        });
        return it == v.begin() ? "" : prev(it)->first;
    }
};

/**
 * 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);
 */
133600