48 主元素Ⅲ
原题网址:https://www.lintcode.com/problem/majority-number-iii/description
描述
给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的1/k。
数组中只有唯一的主元素
您在真实的面试中是否遇到过这个题?
样例
给出数组 [3,1,2,3,2,3,3,4,4,4] ,和 k = 3,返回 3
挑战
要求时间复杂度为O(n),空间复杂度为O(k)
标签
链表
哈希表
思路:
方法同之前的两道主元素题目类似。如果使用哈希表记录数组每个元素出现的次数,返回次数大于 n/k 的元素,时间复杂度为O(n),空间复杂度不为O(k),是多少我也不知道……
AC代码:
class Solution { public: /** * @param nums: A list of integers * @param k: An integer * @return: The majority number */ int majorityNumber(vector<int> &nums, int k) { // write your code here int n=nums.size(); map<int,int> m; for (int i=0;i<n;i++) { if (m.find(nums[i])==m.end()) { m.insert(pair<int,int>(nums[i],1)); } else { (m.find(nums[i])->second)++; } } int result; for (map<int,int>::iterator it=m.begin();it!=m.end();it++) { if (it->second>n/k) { result=it->first; } } return result; } };
我在想统计每个元素出现的次数,返回次数最大的元素可不可以通过……结果还真的AC了。。。囧
代码:
class Solution { public: /** * @param nums: A list of integers * @param k: An integer * @return: The majority number */ int majorityNumber(vector<int> &nums, int k) { // write your code here int n=nums.size(); map<int,int> m; for (int i=0;i<n;i++) { if (m.find(nums[i])==m.end()) { m.insert(pair<int,int>(nums[i],1)); } else { (m.find(nums[i])->second)++; } } int maxCount=0; int result=0; for (map<int,int>::iterator it=m.begin();it!=m.end();it++) { if (it->second>maxCount) { maxCount=it->second; result=it->first; } } return result; } };
挑战版:
想要空间复杂度为O(k),还要使用摩尔投票法,参考 https://www.cnblogs.com/libaoquan/p/7084622.html 这个代码我在Lintcode上跑了下,只通过40%的数据。
自己写了个,只通过47%,想不通为啥……先把代码和运行结果贴上来,以后再研究吧。。。 参考: 正确使用stl map的erase方法
int majorityNumber_k(vector<int> &nums, int k) { int n=nums.size(); map<int,int> m; map<int,int>::iterator it; for (int i=0;i<n;i++) { if (m.size()<k) { if (m.find(nums[i])==m.end()) { m.insert(pair<int,int>(nums[i],1)); } } else { bool flag=false; for (it=m.begin();it!=m.end();it++) { if (m.find(nums[i])==it) { (it->second)++; flag=true; } } if (!flag) { for (it=m.begin();it!=m.end();it++) { (it->second)--; } } for (it=m.begin();it!=m.end();) { if (it->second==0) { m.erase(it++); } else { it++; } } } } for (it=m.begin();it!=m.end();it++) { it->second=0; } for (int i=0;i<n;i++) { if (m.find(nums[i])!=m.end()) { (m.find(nums[i])->second)++; } } int result=0; int maxCount=0; for (it=m.begin();it!=m.end();it++) { if (it->second>maxCount) { maxCount=it->second; result=it->first; } } return result; }
Wrong Answer
输入
[163,96,134,174,69,269,185,41,98,121,10,149,84,311,98,250,162,197,178,94,180,98,96,152,98,222,37,98,175,106,322,98,325,195,294,213,198,105,98,172,309,98,157,204,108,167,98,301,98,162,147,165,98,154,71,335,248,180,265,158,13,211,22,85,254,22,81,110,98,328,52,210,342,49,194,114,333,342,206,166,66,248,83,167,69,98,274,138,63,98,342,178,310,175,126,73,292,282,178,277,291,274,172,40,107,53,98,308,108,209,311,139,255,50,259,10,282,273,32,236,232,98,133,100,60,112,26,341,217,98,141,85,165,286,155,308,115,193,81,330,310,91,47,251,98,76,29,259,10,58,24,128,70,76,56,255,154,276,71,251,105,114,278,238,73,327,63,246,331,336,132,263,71,112,98,204,13,267,283,162,330,138,266,130,314,154,293,198,163,126,84,210,140,117,327,98,130,192,143,173,344,234,234,98,98,41,281,263,199,156,271,338,263,28,340,212,98,269,123,292,71,265,278,266,161,286,299,247,73,10,114,167,131,214,153,340,323,11,98,104,98,198,22,126,76,216,148,238,243,285,98,32,124,161,54,262,190,83,234,98,172,125,33,302,283,297,91,183,102,310,31,147,86,309,78,98,322,216,1 ...
输出
154
期望答案
98
提示