{"id":6471,"date":"2020-03-13T23:21:09","date_gmt":"2020-03-14T06:21:09","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6471"},"modified":"2020-03-13T23:21:49","modified_gmt":"2020-03-14T06:21:49","slug":"leetcode-229-majority-element-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-229-majority-element-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 229. Majority Element II"},"content":{"rendered":"\n<p>Given an integer array of size&nbsp;<em>n<\/em>, find all elements that appear more than&nbsp;<code>\u230a n\/3 \u230b<\/code>&nbsp;times.<\/p>\n\n\n\n<p><strong>Note:&nbsp;<\/strong>The algorithm should run in linear time and in O(1) space.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> [3,2,3]\n<strong>Output:<\/strong> [3]<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> [1,1,1,3,3,2,2,2]\n<strong>Output:<\/strong> [1,2]<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Boyer\u2013Moore Voting Algorithm<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> majorityElement(vector<int>& nums) {\n    int n1 = 0;\n    int c1 = 0;\n    int n2 = 1;\n    int c2 = 0;\n    for (int num : nums) {\n      if (num == n1) {\n        ++c1;\n      } else if (num == n2) {\n        ++c2;\n      } else if (c1 == 0) {\n        n1 = num;\n        c1 = 1;\n      } else if (c2 == 0) {\n        n2 = num;\n        c2 = 1;\n      } else {\n        --c1;\n        --c2;\n      }\n    }\n    \n    c1 = c2 = 0;\n    for (int num : nums) {\n      if (num == n1) ++c1;\n      else if (num == n2) ++c2;\n    }\n    \n    const int c = nums.size() \/ 3;\n    vector<int> ans;\n    if (c1 > c) ans.push_back(n1);\n    if (c2 > c) ans.push_back(n2);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\"># Author: Huahua\nclass Solution:\n  def majorityElement(self, nums: List[int]) -> List[int]:\n    n1, c1, n2, c2 = 0, 0, 1, 0\n    for num in nums:\n      if num == n1: c1 += 1\n      elif num == n2: c2 += 1\n      elif c1 == 0: n1, c1 = num, 1\n      elif c2 == 0: n2, c2 = num, 1\n      else: c1, c2 = c1 - 1, c2 -1\n    \n    c1, c2 = 0, 0\n    for num in nums:\n      if num == n1: c1 += 1\n      elif num == n2: c2 += 1\n    \n    ans = []\n    if c1 > len(nums) \/\/ 3: ans.append(n1)\n    if c2 > len(nums) \/\/ 3: ans.append(n2)\n    return ans\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Related Problem<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><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>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array of size&nbsp;n, find all elements that appear more than&nbsp;\u230a n\/3 \u230b&nbsp;times. Note:&nbsp;The algorithm should run in linear time and in O(1)&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[570,569,177,197],"class_list":["post-6471","post","type-post","status-publish","format-standard","hentry","category-array","tag-boyer-moore","tag-majority-element","tag-medium","tag-voting","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6471"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6471\/revisions"}],"predecessor-version":[{"id":6473,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6471\/revisions\/6473"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}