229. Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.

class Solution {
public:
    vector<int> majorityElement(vector<int>& nums) {
        vector<int> res;

        int cm = 0, cn = 0, m = 0, n = 1;
        for(auto num:nums){
            if (num == m){
                cm++;
            }else if (num == n){
                cn++;
            }else if (cm == 0){
                m = num;
                cm = 1;
            }else if (cn == 0){
                n = num;
                cn = 1;
            }else{
                --cm;
                --cn;
            }
        }

        cm = cn = 0;
        for(auto num:nums){
            if(num == m)
                cm++;
            if(num == n)
                cn++;
        }

        if(cm > nums.size() /3)
            res.push_back(m);
        if(cn > nums.size() /3)
            res.push_back(n);

        return res;
    }
};

results matching ""

    No results matching ""