lintcode 中等题:majority number III主元素III
题目
给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的1/k。
给出数组 [3,1,2,3,2,3,3,4,4,4] ,和 k = ,返回 3
数组中只有唯一的主元素
要求时间复杂度为O(n),空间复杂度为O(k)
解题
上一题刚介绍过所用的方法,但是这个确实很复杂的
先利用HashMap实现
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)
return 0;
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
int majority = 0;
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{
map.put(num,1);
}
if(map.get(num)*k > nums.size()){
majority = num;
break;
}
}
return majority;
}
}
Java Code
总耗时: 1655 ms
class Solution:
"""
@param nums: A list of integers
@param k: As described
@return: The majority number
"""
def majorityNumber(self, nums, k):
# write your code here
if nums == None:
return 0
d = {}
majority = 0
for num in nums:
if num in d:
d[num] += 1
else:
d[num] = 1
if d[num]*k > len(nums):
majority = num
break
return majority
Python Code
总耗时: 307 ms
更新HashMap的方法
import java.util.*;
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,int[]> counters = new HashMap<Integer,int[]>(); // 数组定义key 效率比较高
for(Integer num : nums){
int[] value = counters.get(num);
if(value == null){
counters.put(num,new int[]{1});
}else{
value[0]++; // 直接 + 1
} }
k = nums.size()/k;
for(Map.Entry<Integer,int[]> entry:counters.entrySet()){
int[] value = entry.getValue();
if(value[0] > k){
return entry.getKey();
}
}
return Integer.MAX_VALUE;
}
}
进阶2:思路是,如果出现k个不一样的数,就抵消掉。这里需要用巧妙的数据结构来记录Candidates,并使得如下操作均为O(1):
1. 加入一个Candidate/给某个Candidate出现次数+1
2. Candidates中是否存在某个数
3. Candidates中所有数的出现次数 - 1
4. 移除出现次数为0的Candidates
对于1,2两个操作,我们自然可以想到使用Hash表来完成。对于第4两个操作,我们希望能够有出现次数最少的Candidate的信息,但是如果使用Heap则并非O(1)的时间复杂度。注意到每一次加入一个Candidate时,count均为1,每一次给改变一个Candidate出现次数时,也只涉及到加1运算。因此,如果我们能维护Candidates的有序性,就可以容易的解决这个问题。方法是,使用LinkedList。与普通的LinkedList不同的是,我们将所有出现次数相同的Candidate放在一个Bucket里,Bucket内部的Candidate用Doubly Linked List链接起来,Bucket之间也用Doubly Linked List链接起来。这样针对+1运算,我们只需要通过Hash表找到对应的Candidate,把Candidate从当前的Bucket移动到下一个Bucket(出现次数+1的Bucket)。另外,对于所有数-1的操作,我们记录全局的一个Base,每次-1操作,则Base+1。如果Base和Buckets中的第一个Bucket中的Candidates的出现次数相同,则整个删除第一个Bucket。最后,我们会得到最大k-1个Candidates,重新遍历一遍整个数组,用O(k)的Hash记录这k-1个Candidates的出现次数,就可以验证谁是真正的主元素。
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> counters = new HashMap<Integer,Integer>();
for(Integer num : nums){
if(!counters.containsKey(num)){
counters.put(num,1);
}else{
counters.put(num,counters.get(num) + 1);
}
if( counters.size() >= k){
removeKey(counters);// 清空
} }
if( counters.size() ==0) {
return Integer.MIN_VALUE;
}
for(Integer i: counters.keySet()){
counters.put(i,0);
}
for(Integer i :nums){
if(counters.containsKey(i)){
counters.put(i,counters.get(i) + 1);
}
}
int maxCounter = 0;
int maxKey = 0;
for (Integer i : counters.keySet()) {
if (counters.get(i) > maxCounter) {
maxCounter = counters.get(i);
maxKey = i;
}
} return maxKey;
}
private void removeKey(HashMap<Integer, Integer> counters) {
Set<Integer> keySet = counters.keySet();
List<Integer> removeList = new ArrayList<>();
for (Integer key : keySet) {
counters.put(key, counters.get(key) - 1);
if (counters.get(key) == 0) {
removeList.add(key);
}
}
for (Integer key : removeList) {
counters.remove(key);
}
}
}
Python Code
总耗时: 1725 ms
表示没有理解透彻
lintcode 中等题:majority number III主元素III的更多相关文章
- lintcode 中等题:Majority number II 主元素 II
题目 主元素II 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一. 样例 给出数组[1,2,1,2,1,3,3] 返回 1 注意 数组中只有唯一的主元素 挑战 要求时 ...
- Majority Element:主元素
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【Lintcode】046.Majority Number
题目: Given an array of integers, the majority number is the number that occurs more than half of the ...
- lintcode 中等题:Single number III 落单的数III
题目 落单的数 III 给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到这两个数字. 样例 给出 [1,2,2,3,4,4,5,3],返回 1和5 挑战 O(n)时间复杂度, ...
- lintcode 中等题:subsets II 带重复元素的子集
题目 带重复元素的子集 给定一个可能具有重复数字的列表,返回其所有可能的子集 样例 如果 S = [1,2,2],一个可能的答案为: [ [2], [1], [1,2,2], [2,2], [1,2] ...
- lintcode 中等题:kth-largest-element 第k大元素
题目 第k大元素 在数组中找到第k大的元素 样例 给出数组[9,3,2,4,8],第三大的元素是4 给出数组 [1,2,3,4,5],第一大的元素是5,第二大的元素是4,第三大的元素是3,以此类推 注 ...
- lintcode 中等题:find the missing number 寻找缺失的数
题目 寻找缺失的数 给出一个包含 0 .. N 中 N 个数的序列,找出0 .. N 中没有出现在序列中的那个数. 样例 N = 4 且序列为 [0, 1, 3] 时,缺失的数为2. 注意 可以改变序 ...
- lintcode 中等题:Singleton number II 落单的数 II
题目 落单的数 II 给出3*n + 1 个的数字,除其中一个数字之外其他每个数字均出现三次,找到这个数字. 样例 给出 [1,1,2,3,3,3,2,2,4,1] ,返回 4 挑战 一次遍历,常数级 ...
- lintcode 中等题:Letter Combinations of a Phone Number 电话号码的字母组合
题目 电话号码的字母组合 给一个数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合. 下图的手机按键图,就表示了每个数字可以代表的字母. 样例 给定 "23" 返回 [& ...
随机推荐
- 读取XML
public sealed class ConfigManger { public XDocument XmlDocs { set; get; } string path = @"{0}\C ...
- 基础学习总结(六)--getContentRolver()的应用、内容监听者ContentObserver
ContentResolver / getContentResolver() 外界的程序通过ContentResolver接口可以访问ContentProvider提供的数据,在Activity当 ...
- fastclick插件 导致 input[type="date"] 无法触发问题解决方案
鄙人才疏学浅,新人一枚,不足之处还请谅解,写下这个也只是为了给大家分享一下我解决这个BUG的方法,也是自己的一个笔记. 首先,我们使用fastclick插件的初衷是解决“tap”事件“点透”的BUG: ...
- Yii2框架数据库增删改查小结
User::find()->all(); //返回所有用户数据:User::findOne($id); //返回 主键 id=1 的一条数据: User::find()->wh ...
- vc列表控件的初始化
void CManageProcessDlg::InitList() { m_ListProcess.SetExtendedStyle(m_ListProcess.GetExtendedStyle( ...
- sqlserver中distinct的用法(不重复的记录)
下面先来看看例子: table表 字段1 字段2 id name 1 a 2 b 3 c 4 ...
- jdbc连接数据库使用sid和service_name的区别
问题描述: ORA-12505, TNS:listener does not currently know of SID given in connect descriptor The Connect ...
- SVN四部曲之SVN简单使用教程入门
1. 签出源代码到本机 在本机创建文件夹StartKit,右键点击Checkout,弹出如下图的窗体: 2. 2 在上图中URL of Repository:下的文本框中输 ...
- VirtualBox内Linux系统与Windows共享文件夹
在日常工作或学习中我们经常需要在一台电脑上同时使用Windows和Linux(这里以Ubuntu为例)两个系统,我们通常的做法有两种: 一种安装双系统(双系统的安装方法经验里已经有很多,大家可以去参照 ...
- 在Visual Studio 2010 中创建类库(dll)
创建类库 选择"文件"->新建->项目->Visual C# ->类库,输入名称,选择位置,单击确定 浏览解决方案资源管理器,可以看到两个C#类,第一个是A ...