<?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>binary seach Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/binary-seach/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/binary-seach/</link>
	<description></description>
	<lastBuildDate>Tue, 25 Sep 2018 15:32:52 +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>binary seach Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/binary-seach/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 911. Online Election</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-911-online-election/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-911-online-election/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 25 Sep 2018 15:32:30 +0000</pubDate>
				<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[binary seach]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[online query]]></category>
		<category><![CDATA[vote]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4073</guid>

					<description><![CDATA[<p>Problem n an election, the i-th vote was cast for persons[i] at time times[i]. Now, we would like to implement the following query function: TopVotedCandidate.q(int t) will return the number of the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-911-online-election/">花花酱 LeetCode 911. Online Election</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>n an election, the <code>i</code>-th vote was cast for <code>persons[i]</code> at time <code>times[i]</code>.</p>
<p>Now, we would like to implement the following query function: <code>TopVotedCandidate.q(int t)</code> will return the number of the person that was leading the election at time <code>t</code>.</p>
<p>Votes cast at time <code>t</code> will count towards our query.  In the case of a tie, the most recent vote (among tied candidates) wins.</p>
<div>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false "><strong>Input: </strong><span id="example-input-1-1">["TopVotedCandidate","q","q","q","q","q","q"]</span>, <span id="example-input-1-2">[[[0,1,1,0,0,1,0],[0,5,10,15,20,25,30]],[3],[12],[25],[15],[24],[8]]</span>
<strong>Output: </strong><span id="example-output-1">[null,0,1,1,0,0,1]</span>
<strong>Explanation: </strong>
At time 3, the votes are [0], and 0 is leading.
At time 12, the votes are [0,1,1], and 1 is leading.
At time 25, the votes are [0,1,1,0,0,1], and 1 is leading (as ties go to the most recent vote.)
This continues for 3 more queries at time 15, 24, and 8.
</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>1 &lt;= persons.length = times.length &lt;= 5000</code></li>
<li><code>0 &lt;= persons[i] &lt;= persons.length</code></li>
<li><code>times</code> is a strictly increasing array with all elements in <code>[0, 10^9]</code>.</li>
<li><code>TopVotedCandidate.q</code> is called at most <code>10000</code> times per test case.</li>
<li><code>TopVotedCandidate.q(int t)</code> is always called with <code>t &gt;= times[0]</code>.</li>
</ol>
<h1><strong>Solution: HashTable + Binary Search</strong></h1>
<p>Compute the leads for each t in times using a hash table.</p>
<p>binary search the upper bound of t, and return the lead of previous entry.</p>
<p>Time complexity: Constructor O(n), Query: O(logn)</p>
<p>Space complexity: O(n)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
class TopVotedCandidate {
public:
  TopVotedCandidate(vector&lt;int&gt; persons, vector&lt;int&gt; times) {
    vector&lt;int&gt; votes(persons.size() + 1);
    int last_lead = persons.front();
    for (int i = 0; i &lt; persons.size(); ++i) {
      if (++votes[persons[i]] &gt;= votes[last_lead])
        last_lead = persons[i];      
      leads_[times[i]] = last_lead;      
    }
  }

  int q(int t) {
    return prev(leads_.upper_bound(t))-&gt;second;
  }
private:
  map&lt;int, int&gt; leads_; // time -&gt; lead
};</pre><p></div></div></p>
</div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-911-online-election/">花花酱 LeetCode 911. Online Election</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/algorithms/binary-search/leetcode-911-online-election/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
