leetCode 455. Greedy algorithm for distributing cookies

455. Distributing cookies - LeetCode

Let's say you're a great parent and want to give your kids some cookies . However, no more than one cookie can be given to each child.

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

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, and 3. Although you have two small cookies, since their size is 1, you can only satisfy the child with an appetite value of 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 cookies. The appetite values ​​of the two children are 1 and 2 respectively. The number and size of cookies you have will be enough to satisfy all the kids. So you should output 2.

>>Greedy Ideas (The following text comes from Code Caprice Record (programmercarl.com) )

To satisfy more children, don't waste cookie sizes. Large-sized biscuits can satisfy both children with large appetites and children with small appetites, so priority should be given to those with large appetites. The local optimal here is to feed large biscuits to those with big appetites, making full use of the biscuit size to feed one child, and the global optimal is to feed as many children as possible . You can try to use the greedy strategy and sort the cookie array and child array first. Then traverse the children array from back to front, use large cookies to satisfy those with big appetites first, and count the number of satisfied children.

  • ① Sort
  • ② Determine the traversal order
  • ③ Statistics

  • First traverse the children array, then traverse the cookie array
class Solution {
public:
    // 方法一:
    // 局部最优 : 大饼干喂给胃口大的,充分利用饼干尺寸喂饱一个
    // 全局最优就是喂饱尽可能多的小孩
    // 时间复杂度:O(nlogn) 空间复杂度:O(1)
    int findContentChildren(vector<int>& g, vector<int>& s) {
        sort(g.begin(),g.end());
        sort(s.begin(),s.end());
        int j = s.size()-1;// 饼干数组的下标
        int result = 0;
        for (int i = g.size() - 1; i >= 0; i--) { // 遍历胃口
            if(j>=0 && s[j]>=g[i]) {// 遍历饼干
                result++;
                j--;
            }
        }
        return result;
    }
};
  • Time complexity: O(nlogn)
  • Space complexity: O(1)

  • First traverse the cookie array, then traverse the children array
class Solution {
public:
    // 方法二
    // 小饼干先喂饱小胃口
    // 时间复杂度:O(nlogn) 空间复杂度:O(1)
    int findContentChildren(vector<int>& g, vector<int>& s) {
        sort(g.begin(),g.end());
        sort(s.begin(),s.end());
        int i = 0;// 饼干数组的下标
        int result = 0;
        for (int j = 0; j < s.size(); j++) { // 遍历饼干
            if(i < g.size() && s[j]>=g[i]) { // 遍历胃口
                result++;
                i++;
            }
        }
        return result;
    }
};
  • Time complexity: O(nlogn)
  • Space complexity: O(1)

Screenshot from the Code Caprice class:

Reference and recommended articles and videos

Code Random Record (programmercarl.com)

Greedy algorithm, it is easy to lose sight of the other when considering both! LeetCode: 135. Distribute candy_bilibili_bilibili

Guess you like

Origin blog.csdn.net/weixin_41987016/article/details/133464908