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/ 一开始考虑的方是将所有数转化为二 ...
随机推荐
- com.jcraft.jsch.JSchException: java.io.FileNotFoundException: file:\D:\development\ideaProjects\salary-card\target\salary-card-0.0.1-SNAPSHOT.jar!\BOOT-INF\classes!\keystore\login_id_rsa 资源未找到
com.jcraft.jsch.JSchException: java.io.FileNotFoundException: file:\D:\development\ideaProjects\sala ...
- centos7 安装postgres9.4
1.安装postgres资源:> yum install https://download.postgresql.org/pub/repos/yum/9.4/redhat/rhel-7-x86_ ...
- IIS 配置网站
IIS 配置网站最常见的问题 1 文件夹权限问题 (C盘 windows下temp文件夹权限问题)2 版本问题 (框架版本问题如4.0 在2.0下运行,4.5在4.0下运行) 3如果是局域网,考虑动态 ...
- python中变量的数据类型总结
1.变量的数据类型,分为数值型和非数值型 数值型: int(整型) float(浮点型) bool (布尔型,只有True和Flase) compex(复数型, 用于科学计算) 非数值型: str(字 ...
- 关于spring boot 使用 mybatis plus INSERT的时候id报错
mybatis plus 在INSERT的时候会默认自动设置插入id 我当时数据库采用的id自增. 在使用插入语句的时候并没有set ID 但是它默认给了一大串 更改mybatis plus全局配置 ...
- egret性能优化总结
## 来自官方的优化建议 详见:http://edn.egret.com/cn/article/index/id/287 (1) 少使用Alpha混合. (2) 显式停止计时器,让它们准备好进行垃圾回 ...
- 2018爆零记第二弹之day0
话说初赛水了个70分,ε=(´ο`*)))唉,还是太菜了. 今天两点左右到了电子科大对面宾馆,收拾安顿好后又去电子科大踩点. 进门又走过了不长不短的水杉道,来到了不大不小的西湖(为什么是这个名字... ...
- docker 下载安装与配置
# mac离线安装dockerhttps://download.docker.com/mac/stable/24312/Docker.dmg # windows离线安装dockerhttp://mir ...
- Hyperledger Fabric 1.0 从零开始(一)
在HyperLedger/Fabric发布0.6的时候,公司就已经安排了一个团队研究这一块,后来也请IBM的专家组过来培训了一批人,不幸的是,这批人后来全走了,然后1.0就发布了.自从2017年7月H ...
- tac命令详解
基础命令学习目录首页 原文链接:http://blog.chinaunix.net/uid-128922-id-289974.html 有许多命令都可以查看文件,不同的命令有不同的优点,可以针对不同的 ...