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 of the array.
Find it.
Have you met this question in a real interview? Yes
Example
Given [1, 2, 1, 2, 1, 3, 3], return 1.
Note
There is only one majority number in the array.
Challenge
O(n) time and O(1) extra space.
既然已经到1/3了不如直接用1/K的做法:
class Solution {
public:
/**
* @param nums: A list of integers
* @return: The majority number occurs more than 1/3.
*/
int majorityNumber(vector<int> nums) {
// write your code here
int stored = 2;
unordered_map<int, int> counts;
for (int e : nums) {
if (counts.size() < stored && counts.count(e) == 0) {
counts[e] = 1;
continue;
}
if (counts.count(e) == 0) {
auto iter = counts.begin();
while (iter != counts.end()) {
if (--iter->second == 0) {
iter = counts.erase(iter);
continue;
}
++iter;
}
} else {
counts[e]++;
}
}
int maxcount = -1;
int majority = 0;
for (auto& iter : counts) {
iter.second = 0;
}
for (int e : nums) {
if (counts.count(e)) {
if (++counts[e] > maxcount) {
maxcount = counts[e];
majority = e;
}
}
}
return majority;
}
};
当K=3时,由于数组中总是有数字会超过1/3个,那么我们三个一组三个一组的把(不同)数字依次除去,剩下可能又1个、2个数字的情况,当剩下数字只有一个时该数就是所求的数字,当有两个时我们是不能确定的,需要看已除去组中那个数字出现的比较多才能决定。
算法就是使用哈希来存储K-1个不同数字出现的次数,当有一个数和哈希表中任意K-1一个数都不同时,可以将这K个数作为一组整体划去(两两不同的一组数),体现为将K-1个数的计数减一(因为刚刚那个和K-1数都不同的数并没有存储到哈希表中所以不需要额外动作)。当划去过程中某个数在哈希表中计数值到达零时应该将其删去。最后再对留在哈希表中的数字做一个统计看到底那个数出现的次数多。
因而空间复杂度可以认为是O(K)的
*/
int majorityNumber(vector<int> nums, int k) {
// write your code here
int stored = k - 1;
LintCode Majority Number II / III的更多相关文章
- Lintcode: Majority Number II 解题报告
Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integ ...
- 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 III
Given an array of integers and a number k, the majority number is the number that occurs more than 1 ...
- 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 II 主元素 II
题目 主元素II 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一. 样例 给出数组[1,2,1,2,1,3,3] 返回 1 注意 数组中只有唯一的主元素 挑战 要求时 ...
- 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 求大多数
Given an array of integers, the majority number is the number that occurs more than half of the size ...
- LintCode: Single Number II
一篇解析比较详细的文章:http://www.acmerblog.com/leetcode-single-number-ii-5394.html C++ 解法(1) 求出每个比特位的数目,然后%3,如 ...
随机推荐
- vs2012升级vs2017后的一些坑
异常信息:未能加载文件或程序集"System.Web.Helpers... 未能加载文件或程序集"System.Web.Helpers, Version=2.0.0.0, Cult ...
- IEDA的程序调试debug
以前只是浅层面的使用dubug来查看程序运行顺序,排查一些异常的原因, 今天由于要学习一些源码,所以系统的记录一下(借鉴网上资料总结而来) 主要涉及到的功能区为如下: A::重启项目 快捷键 Ctrl ...
- Java面试题总结(附答案)
1.什么是B/S架构?C/S架构? B/S(Browser/Server),浏览器/服务器程序: C/S(Client/Server),客户端/服务端,桌面应用程序. 2.网络协议有哪些? HTTP: ...
- Day7:html和css
Day7:html和css 如果有浮动,会导致脱标,定位也能脱标,我们没有清除浮动,因为里面有子绝父相. 清除浮动的方法 额外标签法,在最后一个浮动元素后面添加一个空的标签代码: <div st ...
- LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context: Root WebApplicationContext: startup date [Sun Jan 13 17:59:19 CST 2019]; root of context hierarch
在运行项目时出现了:LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via ...
- Kubernetes 服务入口管理 Traefik Ingress Controller
前面部署了 kubernetes/ingress-nginx 作为 Ingress Controller,使用 Nginx 反向代理与负载,通过 Ingress Controller 不断的跟 Kub ...
- 如何正确且高效实现OSSIM中文化的解决方案(图文详解)
前言 对于玩OSSIM的初学者或者中级水平的从业人员来说,都有一定必要性从中文看起,当然,最终还是英文的目标迈进,只是说,为了让自己更快速上手! 虽然系统说明支持中文,实际上,只是台湾的繁体中文而 ...
- TCPWrap的使用配置
参考地址: http://www.softpanorama.org/Net/Network_security/TCP_wrappers/index.shtml http://generationip. ...
- 【原创】驱动卸载之ControlService函数
BOOL WINAPI ControlService( _In_ SC_HANDLE hService, _In_ DWORD dwControl, _Out_ LPSERVICE_STATUS lp ...
- Perl中的hash类型
hash类型 hash类型也称为字典.关联数组.映射(map)等等,其实它们都是同一种东西:键值对.每一个Key对应一个Value. hash会将key/value散列后,按序放进hash桶.散列后的 ...