当前位置:网站首页>Majority element ii[molar voting method for finding modes]
Majority element ii[molar voting method for finding modes]
2022-06-30 00:01:00 【REN_ Linsen】
Moore voting
Preface
seek K The number of , The intuitive approach is hash Record , Then take the largest number K individual . And for mode , The time and space can be reduced to O(n)/O(1).
Of course , The premise is that you can analyze the mode problem , Some questions are implicit .
One 、 Most elements II
Two 、 intuitive hash| Moore voting
1、 intuitive hash
// Every three numbers have it , It means that this number is the most 2 individual , least 0 individual .
// Calculation of violence
public List<Integer> majorityElement(int[] nums) {
int n = nums.length;
int bound = n / 3;
Map<Integer, Integer> fx = new HashMap<>();
List<Integer> ans = new ArrayList<>();
for (int num : nums) fx.put(num, fx.getOrDefault(num, 0) + 1);
for (Map.Entry<Integer, Integer> entry : fx.entrySet()) if (entry.getValue() > bound) ans.add(entry.getKey());
return ans;
}
2、 Moore voting
// Every three numbers have it , It means that this number is the most 2 individual , least 0 individual .
// Since there are 0-2 individual , Just use the voting method first , Select the two most elements , Again for The actual number of circular records , Finally, determine whether the two are greater than n/3, To make sure it's 0/1/2 individual .
public List<Integer> majorityElement2(int[] nums) {
int el1 = 1 << 31, el2 = 1 << 31;// Two elements .
int v1 = 0, v2 = 0;// Number of votes for both
// vote , Determine who the top two elements are ?
for (int num : nums) {
// notes : Here we must first determine whether there are equal .
if (num == el1) ++v1;
else if (num == el2) ++v2;
else if (v1 == 0) {
el1 = num;
++v1;
} else if (v2 == 0) {
el2 = num;
++v2;
} else {
--v1;
--v2;
}
}
// Calculation , Determine the two maximum elements , What is its actual number .
v1 = v2 = 0;
for (int num : nums) {
if (num == el1) ++v1;
if (num == el2) ++v2;
}
// determine , See if the largest element satisfies the condition .
int bound = nums.length / 3;
List<Integer> ans = new ArrayList<>();
if (v1 > bound) ans.add(el1);
if (v2 > bound) ans.add(el2);
// return , Return the element that finally meets the condition .
return ans;
}
summary
1) Moore voting solves a class of mode problems .
reference
边栏推荐
- AI首席架构师9-胡晓光 《飞桨模型库与行业应用》
- [wechat applet] understand the basic composition of the applet project
- How to view the CPU cores and threads in win11? Win11 view the tutorial of how many cores and threads the CPU is
- Unity splashimage scaling problem
- Divisor
- Which securities company should I choose to open an account online? Also, is it safe to open an account online?
- QT learning 05 introduction to QT creator project
- 西门子低代码平台通过Database Connector 连接Mysql 实现增删改查
- Cloner un Graphe non recté [bfs accède à chaque bord et pas seulement aux noeuds]
- New titanium cloud service won the "2022 love analysis · panoramic report of it operation and maintenance manufacturers" cloud management platform CMP representative manufacturer
猜你喜欢
AI赋能新零售,「智」胜之道在于生态思维|数智夜话直播精选摘录
Matlab exercises -- program control process exercise
Cartoon security HIDS, EDR, NDR, XDR
Sword finger offer 14- I. cut rope
How to view the CPU cores and threads in win11? Win11 view the tutorial of how many cores and threads the CPU is
Embedded development: Hardware in the loop testing
500 error occurred after importing skins folder into solo blog skin
由GlideException: Failed DecodePath{DirectByteBuffer->GifDrawable->Drawable}引起的刨根问底
【一起上水硕系列】Day 8
数莓派 4怎么样?可能的玩法有哪些?
随机推荐
数莓派 4怎么样?可能的玩法有哪些?
Yunhe enmo, gaiguoqiang, identify it and grasp it before the domestic database boils
Create an API rapid development platform, awesome!
请指教什么是在线开户?另外,手机开户安全么?
DOM 知识点总结
Koa2 learning and using
6.29日刷题题解
简单理解B树和B+树
After working in the software development industry for six years, I changed my ideas in those years
西门子低代码 9.14版本: 满足不同需求
6.28 problem solving
matlab习题 —— 程序控制流程练习
Shell positional parameter variables and predefined variables
旋转彩色三叶草
solo 博客皮肤导入 skins 文件夹后出现 500 错误
[LeetCode] 只出现一次的数字【136】
Bee常用配置
Copy linked list with random pointer [space for time --hash record]
Yunhe enmo Guoqiang, identifiez - le, saisissez - le, avant l'ébullition de la base de données nationale
Jetpack之Room的使用,结合Flow