2020-12-25Today's share force deduction==455. Distribute cookies

Source: Link: https://leetcode-cn.com/problems/assign-cookies
Statement: If I violate anyone's rights, please contact me and I will delete it.
Welcome experts to spray me

topic

Suppose you are a great parent and want to give your children some cookies. However, each child can only give one cookie at most.

For each child i, there is an appetite value g[i], which is the smallest size of biscuit that can satisfy the appetite of the children; and each biscuit j has a size s[j]. If s[j] >= g[i], we can assign this cookie j to child i, and this child will be satisfied. Your goal is to satisfy as many children as possible and output this maximum value.

Example 1:
Input: g = [1,2,3], s = [1,1]
Output: 1
Explanation:
You have three children and two cookies. The appetite values ​​of the three children are: 1, 2, 3.
Although you have two small biscuits, since their size is 1, you can only satisfy the child whose appetite is 1.
So you should output 1.

Example 2:
Input: g = [1,2], s = [1,2,3]
Output: 2
Explanation:
You have two children and three biscuits, and the appetite values ​​of the two children are 1,2 respectively.
The number and size of cookies you have are enough to satisfy all children.
So you should output 2.

提示:
1 <= g.length <= 3 * 10^4
0 <= s.length <= 3 * 10^4
1 <= g[i], s[j] <= 2^31 - 1

My code double pointer

Here is to sort the two arrays first, the two pointers point to the beginning of the array, and compare them one by one

  • g[i] <= s[j] means that the candy can be allocated to g[i], ret++;
  • g[i]> s[j] means that the candy cannot be divided into g[i], [j++] points to the next candy
class Solution {
    public int findContentChildren(int[] g, int[] s) {
        //Arrays.sort(g);
        quickSort(g, 0, g.length-1);
        quickSort(s, 0, s.length-1);
        //Arrays.sort(s);
        int i=0, j=0;

        int ret=0;
        while(i<g.length && j<s.length){
            if(g[i] <= s[j]){
                ret++;
                i++; j++;
            }else{
                j++;
            }
        }
        return ret;
    }
    public void quickSort(int[] arr, int start, int end){
        if(start >= end) return;
        int p = partition(arr, start, end);
        quickSort(arr, start, p);
        quickSort(arr, p+1, end);
    }
    public int partition(int[]arr, int left, int right){
        int key = arr[left]; 
        while(left < right){
            while(left < right && arr[right] >= key) right--;
            arr[left] = arr[right];
            while(left < right && arr[left] <= key) left ++;
            arr[right] = arr[left]; 
        }
        arr[left] = key;
        return left;
    }
}
Great God's Code

This is the idea,
https://leetcode-cn.com/problems/assign-cookies/solution/fen-fa-bing-gan-by-leetcode-solution-50se/

class Solution {
    public int findContentChildren(int[] g, int[] s) {
        Arrays.sort(g);
        Arrays.sort(s);
        int numOfChildren = g.length, numOfCookies = s.length;
        int count = 0;
        for (int i = 0, j = 0; i < numOfChildren && j < numOfCookies; i++, j++) {
            while (j < numOfCookies && g[i] > s[j]) {
                j++;
            }
            if (j < numOfCookies) {
                count++;
            }
        }
        return count;
    }
}

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/assign-cookies/solution/fen-fa-bing-gan-by-leetcode-solution-50se/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Guess you like

Origin blog.csdn.net/qq_45531729/article/details/111674079