[LeetCode] 169. Majority Element

Description

Given an array of size \(n\), find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.

Example 1:

Input: [3,2,3]
Output: 3

Example 2:

Input: [2,2,1,1,1,2,2]
Output: 2

Analyse

找出一个长为\(n\)的数组中出现次数大于⌊ n/2 ⌋的数

\(n\)不为0
majority element一定存在

第一种方法,majority element的出现次数大于一半,先排序,然后直接取中间的元素,时间复杂度主要是sort的,平均时间复杂度为\(O(Nlog(N))\)

The C standard doesn’t talk about its complexity of qsort. The new C++11 standard requires that the complexity of sort to be O(Nlog(N)) in the worst case. Previous versions of C++ such as C++03 allow possible worst case scenario of O(N^2). Only average complexity was required to be O(N log N).

int majorityElement(vector<int>& nums)
{
    sort(nums.begin(), nums.end());
    return nums[nums.size() / 2];
}

结果被大佬们虐成渣了

Runtime: 28 ms, faster than 28.19% of C++ online submissions for Majority Element.

Memory Usage: 11.1 MB, less than 80.66% of C++ online submissions for Majority Element.

第二种方法,计数法,找个map计算元素出现次数,输出次数大于一半的元素,时间复杂度\(O(n)\)

int majorityElement(vector<int>& nums)
{
    unordered_map<int, int> map;

    for(int a : nums)
    {
        if (++map[a] > (nums.size() / 2))
        {
            return a;
        }
    }

    return -1;
}

比前面那种方法快了一点,但在leetcode上还是不够快,

Runtime: 24 ms, faster than 48.33% of C++ online submissions for Majority Element.

Memory Usage: 11 MB, less than 96.80% of C++ online submissions for Majority Element.

加上一些优化代码后,在leetcode上已经是最快了

static int x=[](){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}();

class Solution {
public:
    int majorityElement(vector<int>& nums)
    {
        unordered_map<int, int> map;

        for(int a : nums)
        {
            if (++map[a] > (nums.size() / 2))
            {
                return a;
            }
        }

        return -1;
    }
};
Runtime: 4 ms, faster than 100.00% of C++ online submissions for Majority Element.

Memory Usage: 11.1 MB, less than 74.76% of C++ online submissions for Majority Element.

第三种方法,来自leetcode,由于majority element的个数大于一半,对数组中不一样的元素两两删除,剩下的就是majority element

直接对vector删除元素很耗时,使用stack来模拟这个删除

int majorityElement(vector<int>& nums)
{
    stack<int> tony;

    for(int a : nums)
    {
        if (tony.empty() || a == tony.top())
        {
            tony.push(a);
        }
        else
        {
            tony.pop();
        }
    }

    return tony.top();
}
Runtime: 8 ms, faster than 99.91% of C++ online submissions for Majority Element.

Memory Usage: 11.7 MB, less than 5.43% of C++ online submissions for Majority Element.

Reference

  1. https://www.geeksforgeeks.org/c-qsort-vs-c-sort/

转载于:https://www.cnblogs.com/arcsinw/p/11301760.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值