<?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>Boyer–Moore Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/boyer-moore/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/boyer-moore/</link>
	<description></description>
	<lastBuildDate>Sat, 14 Mar 2020 06:21:49 +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>Boyer–Moore Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/boyer-moore/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 229. Majority Element II</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-229-majority-element-ii/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-229-majority-element-ii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 14 Mar 2020 06:21:09 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[Boyer–Moore]]></category>
		<category><![CDATA[majority element]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[voting]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6471</guid>

					<description><![CDATA[<p>Given an integer array of size&#160;n, find all elements that appear more than&#160;⌊ n/3 ⌋&#160;times. Note:&#160;The algorithm should run in linear time and in O(1)&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-229-majority-element-ii/">花花酱 LeetCode 229. Majority Element II</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>Given an integer array of size&nbsp;<em>n</em>, find all elements that appear more than&nbsp;<code>⌊ n/3 ⌋</code>&nbsp;times.</p>



<p><strong>Note:&nbsp;</strong>The algorithm should run in linear time and in O(1) space.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> [3,2,3]
<strong>Output:</strong> [3]</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> [1,1,1,3,3,2,2,2]
<strong>Output:</strong> [1,2]</pre>



<h2><strong>Solution: Boyer–Moore Voting Algorithm</strong></h2>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; majorityElement(vector&lt;int&gt;&amp; nums) {
    int n1 = 0;
    int c1 = 0;
    int n2 = 1;
    int c2 = 0;
    for (int num : nums) {
      if (num == n1) {
        ++c1;
      } else if (num == n2) {
        ++c2;
      } else if (c1 == 0) {
        n1 = num;
        c1 = 1;
      } else if (c2 == 0) {
        n2 = num;
        c2 = 1;
      } else {
        --c1;
        --c2;
      }
    }
    
    c1 = c2 = 0;
    for (int num : nums) {
      if (num == n1) ++c1;
      else if (num == n2) ++c2;
    }
    
    const int c = nums.size() / 3;
    vector&lt;int&gt; ans;
    if (c1 &gt; c) ans.push_back(n1);
    if (c2 &gt; c) ans.push_back(n2);
    return ans;
  }
};</pre>
</div></div>



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

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def majorityElement(self, nums: List[int]) -&gt; List[int]:
    n1, c1, n2, c2 = 0, 0, 1, 0
    for num in nums:
      if num == n1: c1 += 1
      elif num == n2: c2 += 1
      elif c1 == 0: n1, c1 = num, 1
      elif c2 == 0: n2, c2 = num, 1
      else: c1, c2 = c1 - 1, c2 -1
    
    c1, c2 = 0, 0
    for num in nums:
      if num == n1: c1 += 1
      elif num == n2: c2 += 1
    
    ans = []
    if c1 &gt; len(nums) // 3: ans.append(n1)
    if c2 &gt; len(nums) // 3: ans.append(n2)
    return ans</pre>
</div></div>



<h2><strong>Related Problem</strong></h2>



<ul><li><a href="https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-169-majority-element/">https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-169-majority-element/</a></li></ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-229-majority-element-ii/">花花酱 LeetCode 229. Majority Element II</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/array/leetcode-229-majority-element-ii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
