I try to solve this problem in terms of probability while compile error says "implicit declaration of function time"


  • 0
    I

    int majorityElement(int* nums, int numsSize) {
    int cnt = (int)(numsSize/2)+1;
    double rate = (double)(cnt*(cnt-1) / (numsSize*(numsSize-1)));

    int count;
    int index1, index2;
    int check_cnt = 0;
    int check_num;
    int check_cnt_sure;
    srand((unsigned)time(NULL));
    
    while(1)
    {
        count = 12;
        while(count>0)
        {
            index1 = rand()%numsSize;
            index2 = rand()%numsSize;
            if(index1!=index2)
            {
                if(nums[index1]==nums[index2])
                {
                    check_num = nums[index1];
                    check_cnt++;
                }
                count--;
            }
        }
        
        if(check_cnt/12 > rate)
        {
            check_cnt_sure = 0;
            for(int i=0;i<numsSize;i++)
            {
                if(nums[i]==check_num)
                    check_cnt_sure++;
            }
            if(check_cnt_sure>=cnt)
                return check_num;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.