Lintcode: Majority Number III
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. Have you met this question in a real interview? Yes
Example
Given [3,1,2,3,2,3,3,4,4,4] and k=3, return 3. Note
There is only one majority number in the array. Challenge
O(n) time and O(k) extra space
这道题跟Lintcode: Majority Number II思路很像,那个找大于1/3的,最多有两个candidate,这个一样,找大于1/k的,最多有k-1个candidate
维护k-1个candidate 在map里面,key为数字值,value为出现次数。先找到这k-1个candidate后,扫描所有元素,如果该元素存在在map里面,update map;如果不存在,1: 如果map里面有值为count= 0,那么删除掉这个元素,加入新元素;2:map里面没有0出现,那么就每个元素的count--
注意:有可能map里有多个元素count都变成了0,只用删掉一个就好了。因为还有0存在,所以下一次再需要添加新元素的时候不会执行所有元素count-1, 而是会用新元素替代那个为0的元素
这道题因为要用map的value寻找key,所以还可以用map.entrySet(), return Map.Entry type, 可以调用getKey() 和 getValue()
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
if (nums==null || nums.size()==0 || k<=0) return Integer.MIN_VALUE;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int i = 0;
for (; i<nums.size(); i++) {
int key = nums.get(i);
if (map.containsKey(key)) {
map.put(key, map.get(key)+1);
}
else {
map.put(key, 1);
if (map.size() >= k) break;
}
}
while (i < nums.size()) {
int key = nums.get(i);
if (map.containsKey(key)) {
map.put(key, map.get(key)+1);
}
else {
if (map.values().contains(0)) { //map contains value 0
map.put(key, 1); // add new element to map
//delete key that has value 0
int zeroKey = 0;
for (int entry : map.keySet()) {
if (map.get(entry) == 0) {
zeroKey = entry;
break;
}
}
map.remove(zeroKey);
}
else {
for (int nonzeroKey : map.keySet()) {
map.put(nonzeroKey, map.get(nonzeroKey)-1);
}
}
}
i++;
}
HashMap<Integer, Integer> newmap = new HashMap<Integer, Integer>();
int max = 0;
int major = 0;
for (int j=0; j<nums.size(); j++) {
int cur = nums.get(j);
if (!map.containsKey(cur)) continue;
if (newmap.containsKey(cur)) {
newmap.put(cur, newmap.get(cur)+1);
}
else {
newmap.put(cur, 1);
}
if (newmap.get(cur) > max) {
major = cur;
max = newmap.get(cur);
}
}
return major;
}
}
Lintcode: 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 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 II 解题报告
Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integ ...
- Lintcode: Majority Number 解题报告
Majority Number 原题链接:http://lintcode.com/en/problem/majority-number/# Given an array of integers, th ...
- [LintCode] Majority Number 求众数
Given an array of integers, the majority number is the number that occurs more than half of the size ...
- Lintcode: Majority Number II
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 ...
- Majority Number III
Given an array of integers and a number k, the majority number is the number that occursmore than 1/ ...
随机推荐
- Ubuntu/Deepin下常用软件汇总(持续更新)
最近开始用Ubuntu了,好多软件都不是常用的了,在这边留底,以免忘记.如果没有写安装方法,则直接在软件源中可以找到 UNetbootin U盘制作工具,制作Ubuntu的安装U盘超好用 Braser ...
- yii多数据库
Yii中同时连接多个数据库 Published by 荒野无灯 on 2011-07-09 02:12:45 under PHP/Yii Tags:yii,database 14162 views 0 ...
- Bluetooth LMP介绍
目录 1. 介绍 2. 数据包格式(Packet Format) 3. Procedure Rules 4. 通用回应消息(General Response Messages) 5. 设备特性(Dev ...
- ZooKeeper Recipes and Solutions 翻译
ZooKeeper 秘诀 与解决方案 A Guide to Creating Higher-level Constructs with ZooKeeper Out of the Box Applica ...
- Java多线程 - 线程状态
转自: http://www.cnblogs.com/lwbqqyumidi/p/3804883.html 一.线程的生命周期及五种基本状态 关于Java中线程的生命周期,首先看一下下面这张较为经典的 ...
- 【指标测试】影响IOPS的几个重要因素
1. 读写方式 顺序读写的IOPS要比随机读写的IOPS高.100%顺序读写来讲,顺序读要高于顺序写.100%随机读写来讲,随机读要高于随机写.小块读写的IOPS要比大块读写高.需要根据实际的应用程序 ...
- Windows7下 配置 Apache + PHP + MySQL + Zend Studio配置
相关软件下载: Apache 版本:(httpd-2.2.25) PHP ...
- debian linux 下安装 netbeans(php)
1.安装netbians依赖jdk7 ,所以第一步是 apt-get install openjdk-7-jre 2.安装netbians 通过wget http://dlc.sun.com.edg ...
- iOS 各尺寸iPhone分辨率
- 实践JAVA wait(), notify(),sleep方法--一道多线程的面试题
建立三个线程,A线程打印10次A,B线程打印10次B,C线程打印10次C,要求线程同时运行,交替打印10次ABC. 这个问题用Object的wait(),notify()就可以很方便的解决. publ ...