Question

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.

Majority Element I

A Linear Time Voting Algorithm

Solution 1 -- Binary Search

我们发现一个规律。如果是大于 1/k 的majority,排好序的数组中,它们可能出现的位置是

len / k, len  * 2/ k, len * 3 / k, ...

所以我们可以依次用二分查找法查找在 len * i / k 的各个点的start和end position,然后算长度,判断是否满足条件。

动态一些的方法是下一个待判断点的坐标为 prevEnd + len / k + 1

如图,绿线为start point 红线为end point

Time complexity O(n log n), space O(1)

 public class Solution {
public List<Integer> majorityElement(int[] nums) {
Arrays.sort(nums);
List<Integer> result = new ArrayList<Integer>();
int len = nums.length;
if (len < 1)
return result;
int candidate1 = nums[len / 3];
// Find start and end of candidate1
int[] range1 = searchRange(nums, candidate1);
int num1 = range1[1] - range1[0] + 1;
if (num1 > len / 3)
result.add(candidate1);
// Find start and end of candidate2
int index = len / 3 + range1[1] + 1;
if (index >= len)
return result;
int candidate2 = nums[index];
int[] range2 = searchRange(nums, candidate2);
int num2 = range2[1] - range2[0] + 1;
if (num2 > len / 3 && candidate2 != candidate1)
result.add(candidate2);
return result;
} private int[] searchRange(int[] nums, int target) {
int start = 0, end = nums.length - 1, mid;
int[] result = new int[2];
result[0] = -1;
result[1] = -1;
while (start + 1 < end) {
mid = (end - start) / 2 + start;
if (nums[mid] >= target)
end = mid;
else
start = mid;
}
if (nums[start] == target)
result[0] = start;
else if (nums[end] == target)
result[0] = end; start = 0;
end = nums.length - 1; while (start + 1 < end) {
mid = (end - start) / 2 + start;
if (nums[mid] > target)
end = mid;
else
start = mid;
}
if (nums[end] == target)
result[1] = end;
else if (nums[start] == target)
result[1] = start;
return result;
}
}

Solution 2 -- Moore Voting Algorithm

Time complexity O(n), space cost O(1)

 public class Solution {
public List<Integer> majorityElement(int[] nums) {
int count1 = 0, count2 = 0;
Integer num1 = null, num2 = null;
int len = nums.length;
for (int current : nums) {
if (num1 != null && current == num1.intValue()) {
count1++;
} else if (num2 != null && current == num2.intValue()) {
count2++;
} else if (num1 == null || count1 == 0) {
num1 = current;
count1 = 1;
} else if (num2 == null || count2 == 0) {
num2 = current;
count2 = 1;
} else {
count1--;
count2--;
}
}
// Check whether num1, num2, num3 are valid
count1 = 0;
count2 = 0;
for (int current : nums) {
if (current == num1.intValue()) {
count1++;
} else if (current == num2.intValue()) {
count2++;
}
}
List<Integer> result = new ArrayList<Integer>();
if (count1 > len / 3) {
result.add(num1);
}
if (count2 > len / 3) {
result.add(num2);
}
return result;
}
}

Majority Element II 解答的更多相关文章

  1. LeetCode(169)Majority Element and Majority Element II

    一个数组里有一个数重复了n/2多次,找到 思路:既然这个数重复了一半以上的长度,那么排序后,必然占据了 a[n/2]这个位置. class Solution { public: int majorit ...

  2. Majority Element,Majority Element II

    一:Majority Element Given an array of size n, find the majority element. The majority element is the ...

  3. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  4. Majority Element(169) && Majority Element II(229)

    寻找多数元素这一问题主要运用了:Majority Vote Alogrithm(最大投票算法)1.Majority Element 1)description Given an array of si ...

  5. 【LeetCode】229. Majority Element II

    Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...

  6. LeetCode169 Majority Element, LintCode47 Majority Number II, LeetCode229 Majority Element II, LintCode48 Majority Number III

    LeetCode169. Majority Element Given an array of size n, find the majority element. The majority elem ...

  7. 【刷题-LeetCode】229. Majority Element II

    Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...

  8. [LeetCode] Majority Element II 求众数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  9. Leetcode: Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

随机推荐

  1. 使用layer显示弹出框笔记

    $.layer({     area : ['200px','auto'], //控制层宽高.当设置为auto时,意味着采用自适应, 当然,对于宽度,并不推荐这样做.例如:area : ['310px ...

  2. 小米路由器mini搭建个人静态网站的方法

    小米路由和小米路由mini从本质上来说差距就在1T的硬盘上,其它并没有明显差别,但是功能却差很多,例如:小米路由有自带的LAMP模式,而小米路由mini则没有,换句话说,其实这个功能是被阉割了,仔细研 ...

  3. iPhone 5,6,6 plus 尺寸

  4. 布局神器:Flexbox

    最近的工作内容大多是移动端网页的开发,百分比布局,Media Queries,Bootstrap等常规的响应式/自适应的开发技术皆一一试过,但觉以上都不够灵活,所以,一直再尝试寻求更加灵活的精确的移动 ...

  5. [Cycle.js] Generalizing run() function for more types of sources

    Our application was able to produce write effects, through sinks, and was able to receive read effec ...

  6. NSLog用法,打印日志

    要输出的格式化占位:   %@ 对象 %d, %i 整数 %u   无符整形 %f 浮点/双字 %x, %X 二进制整数 %o 八进制整数 %zu size_t %p 指针 %e   浮点/双字 (科 ...

  7. openssl 非对称加密算法RSA命令详解

    1.非对称加密算法概述 非对称加密算法也称公开密钥算法,其解决了对称加密算法密钥分配的问题,非对称加密算法基本特点如下: 1.加密密钥和解密密钥不同 2.密钥对中的一个密钥可以公开 3.根据公开密钥很 ...

  8. XMLHttpRequest取得响应

    RresponseText:获得字符串形式的响应数据 responseXML:获得XML形式的响应数据 status和statusText:以数字和文本形式返回HTTP状态码 getAllRespon ...

  9. Sass函数--颜色函数--Opacity函数

    Opacity函数简介 在 CSS 中除了可以使用 rgba.hsla 和 transform 来控制颜色透明度之外,还可以使用 opacity 来控制,只不过前两者只是针对颜色上的透明通道做处理,而 ...

  10. Ubuntu + hadoop2.6.0下安装Hive

    第一步:准备hive和mysql安装包 下载hive 1.1.1 地址:http://www.eu.apache.org/dist/hive/ 下载Mysql JDBC 5.1.38驱动:http:/ ...