[C language] 455 LeetCode brush. Biscuit distribution (E)

Suppose you are a good parent, you want to give your kids some small biscuits. However, each child can only give a maximum of a biscuit. For each child i, appetite has a value gi, which made the children meet the minimum size biscuits appetite; each biscuit and j, have a size sj. If sj> = gi, we can assign the biscuits to the children j i, the child will be met. Your goal is to meet as much as possible the greater the number of children, and the maximum output value.

note:

You can assume that appetite is positive.
A child can only have up to a biscuit.

Example 1:

Input: [2,3], [1,1]

Output: 1

Explanation: 
You have three children and two small biscuits, three children's appetite values are: 1,2,3.
Although you have two small biscuits because of their size are 1, you can only make the child appetite value 1 is satisfied.
So you should output 1.
Example 2:

Input: [1,2], [2,3]

Output: 2

Explanation: 
You have two children and three biscuits, two children's appetite values are 1,2.
The number and size of cookies that you have enough to make all children are met.
So you should output 2.

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/assign-cookies
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Consider seeing an array of sorting, the minimum cookies to give to the smallest child, so this problem requires two qort.

int comq(const void *a, const void *b) {
    return *(int*)a > *(int*)b;
}

int findContentChildren(int* g, int gSize, int* s, int sSize){
    int gidx = 0;
    int sidx = 0;
    int count = 0;
    
    qsort(g, gSize, sizeof(int), comq);
    qsort(s, sSize, sizeof(int), comq);
    
    while ((sidx < sSize) && (gidx < gSize)) {
        
        if(s[sidx] >= g[gidx]) {
            count++;
            sidx++;
            gidx++;
        } else {
            sidx++;
        }
    }
    
    return count;
}

 

Published 149 original articles · won praise 17 · views 110 000 +

Guess you like

Origin blog.csdn.net/jin615567975/article/details/104416619