Majority Element
来源:互联网 发布:异星工厂 mac 0.14 编辑:程序博客网 时间:2024/04/26 04:00
Majority Element
- Runtime: O(n2) — Brute force solution: Check each element if it is the majority element.
- Runtime: O(n), Space: O(n) — Hash table: Maintain a hash table of the counts of each element, then find the most common one.
- Runtime: O(n log n) — Sorting: As we know more than half of the array are elements of the same value, we can sort the array and all majority elements will be grouped into one contiguous chunk. Therefore, the middle (n/2th) element must also be the majority element.
- Average runtime: O(n), Worst case runtime: Infinity — Randomization: Randomly pick an element and check if it is the majority element. If it is not, do the random pick again until you find the majority element. As the probability to pick the majority element is greater than 1/2, the expected number of attempts is < 2.
- Runtime: O(n log n) — Divide and conquer: Divide the array into two halves, then find the majority element A in the first half and the majority element B in the second half. The global majority element must either be A or B. If A == B, then it automatically becomes the global majority element. If not, then both A and B are the candidates for the majority element, and it is suffice to check the count of occurrences for at most two candidates. The runtime complexity, T(n) = T(n/2) + 2n = O(n log n).
- Runtime: O(n) — Moore voting algorithm: We maintain a current candidate and a counter initialized to 0. As we iterate the array, we look at the current element x:
- If the counter is 0, we set the current candidate to x and the counter to 1.
- If the counter is not 0, we increment or decrement the counter based on whether x is the current candidate.
- Runtime: O(n) — Bit manipulation: We would need 32 iterations, each calculating the number of 1's for the ith bit of alln numbers. Since a majority must exist, therefore, either count of 1's > count of 0's or vice versa (but can never be equal). The majority number’s ith bit must be the one bit that has the greater count.
Update (2014/12/24): Improve algorithm on the O(n log n) sorting solution: We do not need to 'Find the longest contiguous identical element' after sorting, the n/2th element is always the majority.
Analysis written by @ts and @1337c0d3r.
0 0
- majority element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- majority element
- Majority Element
- Majority Element
- EJB调用原理分析
- Android Fragment完全解析,关于碎片你所需知道的一切
- windows下svn服务器与web 站点的自动同步更新
- 第9章 顺序容器的操作
- GDAL创建JPG格式图像
- Majority Element
- valueForKey: Vs objectForKey:
- cute python学习日记20141230
- 在 Xcode 中使用 GLEW/GLFW
- kafka2.9.2的分布式集群安装和demo(java api)测试
- [Python]网络爬虫(七):Python中的正则表达式教程
- 算法与数据结构面试题(18)-二叉树镜像
- 青岛地图
- [Python]网络爬虫(六):一个简单的百度贴吧的小爬虫