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/ 一开始考虑的方是将所有数转化为二 ...
随机推荐
- CF 258 D. Little Elephant and Broken Sorting
D. Little Elephant and Broken Sorting 链接 题意: 长度为n的序列,m次操作,每次交换两个位置,每次操作的概率为$\frac{1}{2}$,求m此操作后逆序对的期 ...
- CSS3新增特性详解(一)
注:由于CSS3的新特性较多,所以分两篇博文来说明.第一篇主要包括新的选择器.文字及块阴影.多背景图.颜色渐变.圆角等.第二篇主要细说CSS3的各种动画效果,如:旋转.移动.缩放等,还包括图标字体的应 ...
- 解决shell命令"** is not in the sudoers file..."错误
Linux中新建的普通用户一般不会分配给root权限,每次都su root也太麻烦,可以通过在/etc/sudoers文件中添加当前用户的方式,给当前用户赋予sudo命令的使用权限. # 切换到roo ...
- docker-compose 部署 EFK
信息: Docker版本($ docker --version):Docker版本18.06.1-ce,版本e68fc7a 系统信息($ cat /etc/centos-release):CentOS ...
- Egret入门(一)--简介
关于Egret 构建2D游戏,开源. TS + JS 完成打包后可以转换成HTML5的游戏(跨平台) Egret特点 1. 优秀的设计思想 2. 高效的渲染模块 3. 完善的配套工具 4. 灵活的工作 ...
- PHPCMS增加SEO字段调用
alter table v9_site add site_title_index varchar(255) not null;alter table v9_site add keywords_ind ...
- Java字符串与日期互转
Java字符串与日期的相互转换 1.字符串转日期 字符串的格式与日期的格式一定要对应,并且字符串格式可以比日期格式多,但不能少,数字大小不自动计算日期.其中需要主要大小写 年yyyy 月MM 日dd ...
- 10分钟了解 pandas - pandas官方文档译文 [原创]
10 Minutes to pandas 英文原文:https://pandas.pydata.org/pandas-docs/stable/10min.html 版本:pandas 0.23.4 采 ...
- python初学者随笔Week2
一.集合 去重,把一个列表变成集合,自动去重 关系测试,测试两组数据的交集,并集,差集关系 集合是无序的 集合的操作: list_1 = [1,4,5,7,3,6,7,9] list_1 = set( ...
- linux递归查找文件内容并替换
sed -i 's/原字符串/替换后字符串/g' `grep '搜索关键字' -rl /data/目标目录/ --include "*.html"` 上面是递归查找目录中所有的HT ...