<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>kv Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/kv/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/kv/</link>
	<description></description>
	<lastBuildDate>Sun, 27 Jan 2019 21:55:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>kv Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/kv/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 981. Time Based Key-Value Store</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-981-time-based-key-value-store/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-981-time-based-key-value-store/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 27 Jan 2019 05:49:45 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[kv]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[pair]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4691</guid>

					<description><![CDATA[<p>Create a timebased key-value store class&#160;TimeMap, that supports two operations. 1.&#160;set(string key, string value, int timestamp) Stores the&#160;key&#160;and&#160;value, along with the given&#160;timestamp. 2.&#160;get(string key, int&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-981-time-based-key-value-store/">花花酱 LeetCode 981. Time Based Key-Value Store</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Create a timebased key-value store class&nbsp;<code>TimeMap</code>, that supports two operations.</p>



<p>1.&nbsp;<code>set(string key, string value, int timestamp)</code></p>



<ul><li>Stores the&nbsp;<code>key</code>&nbsp;and&nbsp;<code>value</code>, along with the given&nbsp;<code>timestamp</code>.</li></ul>



<p>2.&nbsp;<code>get(string key, int timestamp)</code></p>



<ul><li>Returns a value such that&nbsp;<code>set(key, value, timestamp_prev)</code>&nbsp;was called previously, with&nbsp;<code>timestamp_prev &lt;= timestamp</code>.</li><li>If there are multiple such values, it returns the one with the largest&nbsp;<code>timestamp_prev</code>.</li><li>If there are no values, it returns the empty string (<code>""</code>).</li></ul>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>inputs = ["TimeMap","set","get","get","set","get","get"], inputs = [[],["foo","bar",1],["foo",1],["foo",3],["foo","bar2",4],["foo",4],["foo",5]]
<strong>Output: </strong>[null,null,"bar","bar",null,"bar2","bar2"]
<strong>Explanation: </strong>&nbsp; 
TimeMap kv; &nbsp; 
kv.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1 &nbsp; 
kv.get("foo", 1);  // output "bar" &nbsp; 
kv.get("foo", 3); // output "bar" since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 ie "bar" &nbsp; 
kv.set("foo", "bar2", 4); &nbsp; 
kv.get("foo", 4); // output "bar2" &nbsp; 
kv.get("foo", 5); //output "bar2" &nbsp; 

</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>inputs = ["TimeMap","set","set","get","get","get","get","get"], inputs = [[],["love","high",10],["love","low",20],["love",5],["love",10],["love",15],["love",20],["love",25]]
<strong>Output: </strong>[null,null,null,"","high","high","low","low"]
</pre>



<p><strong>Note:</strong></p>



<ol><li>All key/value strings are lowercase.</li><li>All key/value strings have&nbsp;length in the range&nbsp;<code>[1, 100]</code></li><li>The&nbsp;<code>timestamps</code>&nbsp;for all&nbsp;<code>TimeMap.set</code>&nbsp;operations are strictly increasing.</li><li><code>1 &lt;= timestamp &lt;= 10^7</code></li><li><code>TimeMap.set</code>&nbsp;and&nbsp;<code>TimeMap.get</code>&nbsp;functions will be called a total of&nbsp;<code>120000</code>&nbsp;times (combined) per test case.</li></ol>



<h2>Solution: HashTable + Map</h2>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua, running time: 200 ms
class TimeMap {
public:
  /** Initialize your data structure here. */
  TimeMap() {}

  void set(string key, string value, int timestamp) {
    s_[key].emplace(timestamp, std::move(value));
  }

  string get(string key, int timestamp) {
    auto m = s_.find(key);
    if (m == s_.end()) return &quot;&quot;;    
    auto it = m-&gt;second.upper_bound(timestamp);
    if (it == begin(m-&gt;second)) return &quot;&quot;;
    return prev(it)-&gt;second;
  }
private:
  unordered_map&lt;string, map&lt;int, string&gt;&gt; s_; 
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-981-time-based-key-value-store/">花花酱 LeetCode 981. Time Based Key-Value Store</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/hashtable/leetcode-981-time-based-key-value-store/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
