Question: 

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.

Find it.

Example:

Given [3,1,2,3,2,3,3,4,4,4] and k=3, return 3

Analysis:
根据题目知道,majority number在数组中的个数要大于 n / k,所以最多只可能有k - 1个majority number(如果有k个,那么k * n / k 大于n,显然是不可能的)。维护一个size是k - 1的HashMap,key是数组中的值,value是该值在数组中的个数。遍历整个数组,如果该值已经在map中,则相对应的value加1。如果不存在,那么先检查map的size是否小于k - 1。是的话将该值添加进map中,然后value设为1。否则我们需要将map中所有key对应的value都减去1,如果value降到0,则从map中删去这个key。当整个数组遍历完,map中的值就是所有的可能的值。
这道题只要找唯一一个值,所以我们遍历map中的所有key,找到value最大的那个key就是我们要找的。
Code:
 1 public class Solution {
 2     /**
 3      * @param nums: A list of integers
 4      * @param k: As described
 5      * @return: The majority number
 6      */
 7     public int majorityNumber(ArrayList<Integer> nums, int k) {
 8         if(nums == null || nums.size() == 0) {
 9             return Integer.MIN_VALUE;
10         }
11
12         //most k - 1 values in the map
13         HashMap<Integer, Integer> counters = new HashMap<Integer, Integer>();
14         for(int num : nums) {
15             if(counters.containsKey(num)) {
16                 counters.put(num, counters.get(num) + 1);
17             }else {
18                 if(counters.size() < k - 1) {
19                     counters.put(num, 1);
20                 }else {
21                     update(counters);
22                 }
23             }
24         }
25
26         //corner case
27         if(counters.size() == 0) {
28             return Integer.MIN_VALUE;
29         }
30
31         //clear count
32         for(int key : counters.keySet()) {
33             counters.put(key, 0);
34         }
35
36         //recalculate the counter
37         for(int num : nums) {
38             if(counters.containsKey(num)) {
39                 counters.put(num, counters.get(num) + 1);
40             }
41         }
42
43         int result = 0, maxCount = Integer.MIN_VALUE;
44         for(int key : counters.keySet()) {
45             int count = counters.get(key);
46             if(count > maxCount) {
47                 result = key;
48                 maxCount = count;
49             }
50         }
51
52         return result;
53     }
54
55     private void update(HashMap<Integer, Integer> counters) {
56         ArrayList<Integer> removeList = new ArrayList<Integer>();
57         for(int key : counters.keySet()) {
58             counters.put(key, counters.get(key) - 1);
59             if(counters.get(key) == 0) {
60                 removeList.add(key);
61             }
62         }
63
64         for(int key : removeList) {
65             counters.remove(key);
66         }
67     }
68 }

Complexity:
时间复杂度是O(n * k)。

Reference:
http://www.geeksforgeeks.org/given-an-array-of-of-size-n-finds-all-the-elements-that-appear-more-than-nk-times/

转载于:https://www.cnblogs.com/billzhou0223/p/5150145.html

Majority Number III相关推荐

  1. 【Lintcode】046.Majority Number

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

  2. leetcode majority number

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

  3. leetcode:Majority Number

    1.Given an array of integers, the majority number is the number that occursmore than half of the siz ...

  4. 【?异或】LeetCode 260. Single Number III

    LeetCode 260. Single Number III Solution1: 博客转载自:http://www.cnblogs.com/grandyang/p/4741122.html 这道题 ...

  5. leetcode 260. Single Number III | 260. 只出现一次的数字 III(位运算:分组异或)

    题目 https://leetcode.com/problems/single-number-iii/ 题解:分组异或 参考1:讨论区题解 you know you can eliminate dou ...

  6. leetcode 算法解析(一):260. Single Number III(C++版本和自己的注解)

    这个题来自<剑指offer>但是书上上感觉讲解不太详细,还是看博客吧(我把下面博客改写成了C++版本运行通过) 注意这个题的相关代码中,输入的数组只能有两个数出现一次,如果有第三个数出现一 ...

  7. lintcode 中等题:Single number III 落单的数III

    题目 落单的数 III 给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到这两个数字. 样例 给出 [1,2,2,3,4,4,5,3],返回 1和5 挑战 O(n)时间复杂度, ...

  8. LeetCode Single Number III(位操作)

    问题:给出一个数组,有两个数只出现一次,其它都出现两次. 思路:先对数组求异或,得到两个数异域的结果.然后确定差异的最低位.再将数组与这个差异位作异域,得到其中的一个数.再与异域结果异域后即得到另外一 ...

  9. 【LeetCode】-- 260. Single Number III

    问题描述: https://leetcode.com/problems/single-number-iii/ 在一个数组里面,只有两个元素仅出现过1次,其余都出现过两次.找出出现仅一次的那两个(a, ...

  10. 260. Single Number III

    题目: Given an array of numbers nums, in which exactly two elements appear only once and all the other ...

最新文章

  1. 任正非签发最新电邮:过去我们是为了赚点小钱,现在是要战胜美国
  2. UA PHYS515A 电磁理论IV 时变电磁场理论6 用含时Green函数求解时变电磁场问题的例子
  3. Shiro框架:缓存、session会话、自定义FormAuthenticationFilter、RemenberMe
  4. 如何检查数字是否为2的幂
  5. Python模块的使用
  6. IE6下实现Width:auto
  7. 【预测模型】自回归(AR)模型
  8. 计算机网络之TCP报文
  9. WPF 一个简单的颜色选择器
  10. 检验检测系统管理服务器,检验检测管理平台
  11. Coursera Big Data系列课程笔记1
  12. B站笔试真题之[编程题]扭蛋机
  13. win10千万不要重置_Win10系统如何自动恢复到出厂设置,小白式操作教程
  14. gensim训练wiki中文词向量
  15. 糜烂性胃炎吃什么药?
  16. 性能服务器漫画免费下拉式,奇妙漫画免费漫画
  17. 屏幕挂灯的智商税- 小米真不香
  18. 用Python写一个新年倒计时
  19. linux 游戏下载论坛,LINUX下的各种游戏
  20. 一步步教你怎么用Python写贪吃蛇游戏

热门文章

  1. DevOps使用教程 华为云(19)git diff查看刚刚更新的文件的差异
  2. 第一类丢失更新和第二类丢失更新是什么 区别
  3. ios低版本、微信浏览器低版本 不能使用的js语法总结
  4. python输出特征相关矩阵_两个特征矩阵的有效成对相关
  5. Php的入栈,PHP实现的栈数据结构示例【入栈、出栈、遍历栈】
  6. centos 编译Qt5 mysql驱动_centos7安装编译mysql的驱动的问题
  7. java基础总结03-进制
  8. LocalDateTime时间加减法
  9. C语言w10输入法打不出中文,win10系统输不了中文怎么办
  10. npm安装vue-cli时速度慢,fetchMetadata经常卡住并报异常