Majority Number I & || && |||
Majority Number
Given an array of integers, the majority number is the number that occurs more than half
of the size of the array. Find it.
Example
Given [1, 1, 1, 1, 2, 2, 2]
, return 1
分析:
既然这里只有一个majority number,那么它的个数减去其它number个数之和还是为正值。
public class Solution {
/**
* cnblogs.com/beiyeqingteng/
*/
public int majorityNumber(ArrayList<Integer> nums) {
int number = nums.get();
int count = ; for (int i = ; i < nums.size(); i++) {
if (count == ) {
number = nums.get(i);
count = ;
} else {
if (number == nums.get(i)) {
count++;
} else {
count--;
}
}
}
return number;
}
}
Majority Number II
Given an array of integers, the majority number is the number that occurs more than 1/3
of the size of the array.
Example
Given [1, 1, 1, 1, 2, 2, 2]
, return 1。
- (case 1) If it is one of the majority number candidates, it votes positive to itself, otherwise
- (case 2) If there is one available majority number slot, it gets the slot and votes positive for itself,
- (case 3) otherwise, it votes negative to both majority candidates.
At last, at least one of the two majority numbers must be more than 1 / 3 of the array.
public class Solution {
/**
* @param nums: A list of integers
* @return: The majority number that occurs more than 1/3
* cnblogs.com/beiyeqingteng/
*
*/
public int majorityNumber(ArrayList<Integer> nums) {
if (nums == null || nums.size() == ) return -;
int number1 = , number2 = , count1 = , count2 = ; for (Integer i : nums) {
if (number1 == i) {// case 1
count1++;
} else if (number2 == i) {// case 1
count2++;
} else if (count1 == ) {// case 2
number1 = i;
count1 = ;
} else if (count2 == ) {// case 2
number2 = i;
count2 = ;
} else { // case 3
count1--;
count2--;
}
} // [1,1,1,1,2,2,3,3,4,4,4] cannot pass if the code below is not added.
count1 = ;
count2 = ; for (Integer i : nums) {
if (number1 == i) {
count1++;
} else if (number2 == i) {
count2++;
}
} return count1 > count2 ? number1 : number2;
}
}
Majority Number III
Given an array of integers and a number k, the majority number is the number that occurs more than 1/k
of the size of the array.
Example
Given [3,1,2,3,2,3,3,4,4,4]
and k=3
, return 3
.
分析:
Same as above, as there could be at most (k – 1) elements occuring more than 1 / k of the array, we have (k – 1) slots for majority number candidates. The voting rule is the same as above.
Careful for the ConcurrentModificationException in HashMap, we should remove (write) the keys during the HashMap being iterated (read). Write the hashmap after read.
public class Solution {
/**
* @param nums: A list of integers
* @param k: As described
* @return: The majority number
* cnblogs.com/beiyeqingteng/
*/
public int majorityNumber(ArrayList<Integer> nums, int k) {
if (nums == null || nums.size() == || k < ) return -;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int n : nums) {
if (map.containsKey(n)) {
map.put(n, map.get(n) + );
} else {
// note: if we change condition to map.size() > k - 1, in this case, we assume
// there are at most k candidates, not k - 1, we can ignore the statements from
// line 27 - 35
if (map.size() >= k - ) {
decreaseVotes(map);
} else {
map.put(n, );
}
}
} for (int key : map.keySet()) {
map.put(key, );
} for (int n : nums) {
if (map.containsKey(n)) {
map.put(n, map.get(n) + );
}
} int maxKey = ;
int maxCount = ;
for (int key : map.keySet()) {
if (map.get(key) > maxCount) {
maxCount = map.get(key);
maxKey = key;
}
}
return maxKey; } private void decreaseVotes(Map<Integer, Integer> map) {
Set<Integer> keySet = map.keySet();
List<Integer> removeList = new ArrayList<>();
for (Integer key : keySet) {
if (map.get(key) == ) {
removeList.add(key);
}
else {
map.put(key, map.get(key) - );
}
}
//remove candidates with 0 votes and free the slot
for (Integer key : removeList) {
map.remove(key);
}
}
}
Reference:
http://blog.welkinlan.com/2015/05/29/majority-number-lintcode-java/
Majority Number I & || && |||的更多相关文章
- [LintCode] Majority Number 求众数
Given an array of integers, the majority number is the number that occurs more than half of the size ...
- Lintcode: Majority Number III
Given an array of integers and a number k, the majority number is the number that occurs more than 1 ...
- Lintcode: Majority Number II
Given an array of integers, the majority number is the number that occurs more than 1/3 of the size ...
- leetcode majority number
给定一组数,有一个数在这组数里的出现次数超过n/2次. 求出这是哪个数 https://leetcode.com/problems/majority-element/ 一开始考虑的方是将所有数转化为二 ...
- lintcode 中等题:majority number III主元素III
题目 主元素 III 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的1/k. 样例 ,返回 3 注意 数组中只有唯一的主元素 挑战 要求时间复杂度为O(n),空间复杂度为O( ...
- lintcode 中等题:Majority number II 主元素 II
题目 主元素II 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一. 样例 给出数组[1,2,1,2,1,3,3] 返回 1 注意 数组中只有唯一的主元素 挑战 要求时 ...
- LintCode Majority Number II / III
Given an array of integers, the majority number is the number that occurs more than 1/3 of the size ...
- [LintCode] Majority Number 求大多数
Given an array of integers, the majority number is the number that occurs more than half of the size ...
- Lintcode: Majority Number II 解题报告
Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integ ...
随机推荐
- MIT Scheme 使用 Edwin
MIT Scheme 的基本使用:http://www.math.pku.edu.cn/teachers/qiuzy/progtech/scheme/mit_scheme.htm 安装过程 安装bre ...
- sql语句积累
有一个需求表(demand),每一记录就是一条需求:另外有一个报价表(quotation),每一条记录是对需求记录的报价详情. 需求表: 报价表: 我现在想得到每条需求的信息以及有多少人报价了,我们可 ...
- 如何配置virtualBox端口转发
1,第一步登陆虚拟主机,安装openssh-server(这一步非常重要,如果不安装,你在宿主机上怎么链接都是连不上的,我当时就犯了这个错误) apt-get install openssh-serv ...
- C语言中的EOF和回车不一样
经常我们碰到这样一个C语言问题,例如: 输入一个组整数,按照从小到大排序后输出结果 输入: 1 7 9 2 4 输出: 1 2 4 7 9 这里要用C语言读入一段数的话,如果用 int array ...
- Linux内核循环链表经典分析和移植
为什么说这个链表做的经典呢,哥哥我从Linux内核里边儿扣出来的,要么怎么说内核不是一般人能写的,这代码太TM优美了! 这里有一篇参考文章:http://isis.poly.edu/kulesh/st ...
- WebService 之 WSDL文件 讲解
原文地址:http://blog.csdn.net/tropica/archive/2008/11/02/3203892.aspx 恩,我想说的是,是不是经常有人在开发的时候,特别是和第三方有接口的时 ...
- 遍历一个类的属性--并转换为Dictionary类型
参考地址...http://www.cnblogs.com/xwgli/p/3306297.html 记录点滴...以前很少用泛型...HaHa... /// <summary> /// ...
- 使用LIBSVM工具实现样本分类预测——MatLab
准备工作: https://www.csie.ntu.edu.tw/~cjlin/libsvm/,下载LIBSVM:(LIBSVM工具相较于MATLAB自带的工具:1).支持多分类及回归(‘-s 0’ ...
- Oracle卸载
用Oracle自带的卸载程序不能从根本上卸载Oracle,从而为下次的安装留下隐患,所以要想完全卸载Oracle就必须要直接将注册表清除. 步骤如下: 1. 开始->设置->控制面板-&g ...
- java系列-安装MySql(三)
第一大步:MySQL安装文件分为两种,一种是msi格式的,一种是zip格式的.如果是msi格式的可以直接点击安装,按照它给出的安装提示进行安装(相信大家的英文可以看懂英文提示),一般MySQL将会安装 ...