Majority Number III
Given an array of integers and a number k, the majority number is the number that occursmore than 1/k of the size of the array.
Find it.
Given [3,1,2,3,2,3,3,4,4,4] and k=3, return 3.
There is only one majority number in the array.
O(n) time and O(k) extra space
对于1/k 的数,每当要删除的时候,要把k的数字同时删掉,这样不会影响最终的结果。
public class Solution {
/**
* @param nums: A list of integers
* @param k: As described
* @return: The majority number
*/
public int majorityNumber(ArrayList<Integer> nums, int k) {
// write your code
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i = 0; i < nums.size(); i ++){
int num = nums.get(i);
if(map.containsKey(num)){
map.put(num, map.get(num) + 1);
}else{
if(map.size() < 3){
map.put(num, 1);
}else{//decrease every count by one, remove pair which count is 0
ArrayList<Integer> removeKey = new ArrayList<Integer>();
for (Map.Entry en : map.entrySet()){
en.setValue((int)en.getValue() - 1);
if((int)en.getValue() == 0){
removeKey.add((int)en.getKey());
}
}
for(int j = 0; j < removeKey.size(); j ++){
map.remove(removeKey.get(j));
}
}
}
}
int result = 0;
int resultValue = 0;
for (Map.Entry cur : map.entrySet()){
if((int)cur.getValue() > resultValue){
result = (int)cur.getKey();
resultValue = (int)cur.getValue();
}
}
return result;
}
}
Majority Number III的更多相关文章
- 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 ...
- lintcode 中等题:majority number III主元素III
题目 主元素 III 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的1/k. 样例 ,返回 3 注意 数组中只有唯一的主元素 挑战 要求时间复杂度为O(n),空间复杂度为O( ...
- Lintcode: Majority Number III
Given an array of integers and a number k, the majority number is the number that occurs more than 1 ...
- Majority Number I & || && |||
Majority Number Given an array of integers, the majority number is the number that occurs more than ...
- 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 ...
- leetcode-Single Number III 找独数
Single Number III Given an array of numbers nums, in which exactly two elements appear only once and ...
- 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/ 一开始考虑的方是将所有数转化为二 ...
随机推荐
- Python 学习 第五篇:语句和语法
Python程序是语句构成的,语句包含表达式,表达式嵌套在语句中,包含变量和常量,用于处理对象.Python的语法实质上是由表达式.语句和代码块构成的.语句是由表达式构成的,代码块是由多个语句构成的复 ...
- [VB.NET][C#]二维向量的基本运算
前言 在数学中,几何向量指具有大小(Magnitude)和方向的几何对象,它在线性代数中经由抽象化有着更一般的概念.向量在编程中也有着及其广泛的应用,其作用在图形编程和游戏物理引擎方面尤为突出. 基于 ...
- javascript中encodeURI和decodeURI方法使用介绍
encodeURI和decodeURI是成对来使用的,因为浏览器的地址栏有中文字符的话,可以会出现不可预期的错误, 所以可以encodeURI把非英文字符转化为英文编码,decodeURI可以用来把字 ...
- 使用efwplusScript开发Winform程序【像小程序那样开发PC软件】
一.前言 本人从事多年医疗管理软件的开发,在医院大多数的软件都还是CS程序,BS程序很少,对于使用者来说CS的操作体验确实比BS的要好. 1.CS的缺点就是升级麻烦,每次有新版本都需要所有客户端操作升 ...
- halcon算子之tuple_gen_const,用于生成特定长度的元组并且初始化其元素
原文地址:http://blog.sina.com.cn/s/blog_d38f8be50102wczk.html 函数原型: tuple_gen_const(: : Length, Const : ...
- Python中的常规习题
循环总结 while 语句 for 语句 - 字符串 - range() 函数 break 语句 continue 语句 学习笔记传送门 列表学习 # 练习: # 输入一个整数n, 判断这个整数是否是 ...
- Jmeter关联处理
采桑子·重阳 人生易老天难老, 岁岁重阳. 今又重阳, 战地黄花分外香. 一年一度秋风劲, 不似春光. 胜似春光, 廖廓江天万里霜. 当请求之间有依赖关系,比如一个请求的入参是另一个请求返回的数据,这 ...
- ngxin 添加模块
if test -n "$NGX_ADDONS"; then echo configuring additional modules for ngx_addon_dir in $N ...
- Centos7 安装与破解 Confluence 6.7.1
1.1硬件需求建议: CPU:32/64 bit 2.27GHz双核心以上之CPU: 内存:8GB以上: 硬盘:300GB,7200转以上: 建议数据库.Confluence等各自独立一台服务器(本测 ...
- django orm 操作表
django orm 操作表 1.基本操作 增 models.Tb1.objects.create(c1='xx', c2='oo') 增加一条数据,可以接受字典类型数据 **kwargs inser ...