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/ 一开始考虑的方是将所有数转化为二 ...
随机推荐
- Properties、ResourceBundle
两个类都可以读取属性文件中以key/value形式存储的键值对,ResourceBundle读取属性文件时操作相对简单. Properties 该类继承Hashtable,将键值对存储在集合中.基于输 ...
- 在Docker中安装和部署MongoDB集群
此文已由作者袁欢授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 在Docker中安装mongodb 采用的mongodb镜像:https://registry.hub.doc ...
- jenkins maven设置settings.xml
环境:jenkins.2.89.3 1.安装settings.xml管理插件Config File Provider Plugin 系统管理->管理插件->搜索Config File P ...
- Flutter - 创建横跨所有页面的侧滑菜单
前一篇博客讲到了如何创建侧滑菜单,但是再实际使用过程中,会发现,这个策划菜单只能在首页侧滑出来. 当导航到其他页面后,侧滑就不管用了.这也有点不符合良好的用户体验设计.Google Play就是很好的 ...
- 新建React Native项目步骤
根据官方环境 https://reactnative.cn/docs/getting-started/ 搭建好之后 1.新建项目 打开React Native 命令行工具,并输入 react-nati ...
- MySQLConnector/ODBC 安装时遇到的小问题
今天在新做的 Win2008R2 上想使用 SqlDbx 管理 MySQL,提示需要安装 MySQLConnector/ODBC,这没什么,以前装过的,按要求下载安装一个就是了. 结果在安装 MySQ ...
- 什么是REST,RESTful?
转载自https://www.zhihu.com/question/28557115 https://blog.csdn.net/hjc1984117/article/details/77334616 ...
- Proe/Creo 零件库mnu文件制作批处理
proe零件库自定义时需要菜单文件mnu,百度了下网上还没有人制作,偶然间Google时在PTC论坛上看到一德国人分享了自己制作的bat文件用于对文件夹(及子文件夹)产生mnu文件,我在将他的文件翻译 ...
- RabbitMQ入门:总结
随着上一篇博文的发布,RabbitMQ的基础内容我也学习完了,RabbitMQ入门系列的博客跟着收官了,以后有机会的话再写一些在实战中的应用分享,多谢大家一直以来的支持和认可. RabbitMQ入门系 ...
- List集合中的对象进行排序
类A: public class A implements Comparable<A>{ private Integer id; private String name; public A ...