【leetcode】非递增顺序的最小子序列

 

int cmp(const void* a, const void* b){
    
    return *(int*)b - *(int*)a;
}
int* minSubsequence(int* nums, int numsSize, int* returnSize){
    qsort(nums,numsSize,sizeof(int),cmp);
    int i,sum=0;
    for (i=0; i<numsSize; i++) sum+=nums[i];
    float mid = sum/2.0;
    for (i=0; i<numsSize; i++){
        sum-=nums[i];
        if (sum < mid)
        {
            *returnSize=i+1;
            break;
        }
    }
    return nums;
}

 

posted @ 2020-09-17 00:03  温暖了寂寞  阅读(129)  评论(0编辑  收藏  举报