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。

分析:
As there could be at most 2 elements occuring more than 1 / 3 of the array, we have 2 slots for majority number candidates. Each number votes like this:
  • (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 & || && |||的更多相关文章

  1. [LintCode] Majority Number 求众数

    Given an array of integers, the majority number is the number that occurs more than half of the size ...

  2. Lintcode: Majority Number III

    Given an array of integers and a number k, the majority number is the number that occurs more than 1 ...

  3. Lintcode: Majority Number II

    Given an array of integers, the majority number is the number that occurs more than 1/3 of the size ...

  4. leetcode majority number

    给定一组数,有一个数在这组数里的出现次数超过n/2次. 求出这是哪个数 https://leetcode.com/problems/majority-element/ 一开始考虑的方是将所有数转化为二 ...

  5. lintcode 中等题:majority number III主元素III

    题目 主元素 III 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的1/k. 样例 ,返回 3 注意 数组中只有唯一的主元素 挑战 要求时间复杂度为O(n),空间复杂度为O( ...

  6. lintcode 中等题:Majority number II 主元素 II

    题目 主元素II 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一. 样例 给出数组[1,2,1,2,1,3,3] 返回 1 注意 数组中只有唯一的主元素 挑战 要求时 ...

  7. 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 ...

  8. [LintCode] Majority Number 求大多数

    Given an array of integers, the majority number is the number that occurs more than half of the size ...

  9. Lintcode: Majority Number II 解题报告

    Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integ ...

随机推荐

  1. OC-方法的声明和实现、匿名对象

    方法声明: 方法调用: *冒号也是方法名的一部分 *同一个类中不允许两个对象方法同名 练习 给Car类设计一个方法,用来和其他车比较车速,如果快返回1,慢返回-1,相同返回0 #import < ...

  2. PHP笔记

    <?php//统计访问量 if(!@$fp=fopen("num.txt","r")){ echo "num.txt文件创建成功!<br& ...

  3. 深入理解Spring Redis的使用 (一)、Spring Redis基本使用

    关于spring redis框架的使用,网上的例子很多很多.但是在自己最近一段时间的使用中,发现这些教程都是入门教程,包括很多的使用方法,与spring redis丰富的api大相径庭,真是浪费了这么 ...

  4. thinkphp- 许愿墙-1

    控制器的方法, 要显示的模板默认的跟方法名相同. 也可以不同, 但应该 仍然是对应文件夹下的html模板文件: $this->display('其他的模板html文件名, 不用加html扩展名' ...

  5. Unity API

    关于 int Mathf.PingPong(t, length); 原理,相当于 #include <iostream> #include <vector> int test( ...

  6. VTK初学一,e_Triangle三角形的绘制

    #ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> VTK_MODULE_INIT(vtkRend ...

  7. JLS(Third Edition) Chapter12 Execution

    这一章详细说明在一个program执行时,发生的activities. 它根据JVM和组成program的类.接口.实例的生命周期 组织.   一个JVM从加载一个特定的类并调用它的main方法开始启 ...

  8. activti表结构

    1.结构设计 1.1.    逻辑结构设计 Activiti使用到的表都是ACT_开头的. ACT_RE_*: ’RE’表示repository(存储),RepositoryService接口所操作的 ...

  9. protect和private 的区别

    protect和private 的区别 public 表示全局,类内部外部子类都可以访问: private表示私有的,只有本类内部可以使用: protected表示受保护的,只有本类或子类或父类中可以 ...

  10. mybatis 添加事物后 无法获取自增主键的问题

    检查代码后没发现mapper文件设置自增主键返回的问题,后来检查到,关闭事务后,执行完是可以获取返回的主键的, 我在mysql的客户端里关闭自动提交,发现使用select last_insert_id ...