[Leetcode] How many children can you feed with the detailed explanation of Yuan Chu’s daily selected questions?

Title description: 455. Distribute cookies

Example 1:

输入: g = [1,2,3], s = [1,1]
输出: 1

Explanation:
You have three children and two biscuits. The appetite values ​​of the three children are: 1, 2, and 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:

输入: g = [1,2], s = [1,2,3]
输出: 2

Explanation:
You have two children and three biscuits. 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.

Problem analysis:

Just looking at the text may not be particularly easy to understand. Let's analyze the requirements of the title. The length of the array g represents the number of children, and the elements in the array represent the appetite of each child. The length of the s array represents the number of cookies. The elements in the array represent the size of the cookies. We need to feed the children with biscuits, but a child can only get one cookie. So what is considered to be feeding the child? For example, the child’s appetite is 2, and the biscuit size>=2, the child can be fed.

Note: A child can only have one biscuit, this is not possible, g=[3], s=[1,2] will output 0 at this time, because it can only be divided into one piece and cannot be accumulated.

Two-pointer solution:

We first sort the arrays, both arrays need to be sorted (because we should feed children with small appetites first, so as to maximize), then we will encounter the following three situations

(1) If the appetite is greater than the biscuits, you need to replace the biscuits with larger ones to feed him (yellow pointer moves, blue pointer does not move)

(2) If the appetite is equal to the biscuit, give him this biscuit and continue to feed the next child (both pointers remain unchanged)

(3) If the appetite is smaller than the biscuit, give him this biscuit and continue to feed the next child (both pointers remain unchanged)

The following is an explanation animation,

Insert picture description here

Detailed code:

In order to better explain the idea, the code is not simplified, you can read it carefully according to the comments.

class Solution {
    
    
    public int findContentChildren(int[] g, int[] s) {
    
    
          //特殊情况,如果有一方为空数组的情况
          if(s.length == 0 || g.length == 0){
    
    
              return 0;
          }
          //先对数组排序
          Arrays.sort(g);
          Arrays.sort(s);
          int j = 0;//数组s的下标
          int count = 0;//喂孩子的个数
          //遍历数组g,已经排序过的数组g,孩子的胃口数组
          for(int i = 0 ; i < g.length;i++){
    
     
              //首先要防止溢出的情况,其次小于等于的时候符合,数量加一 两个指针同时移动
              //但是我们只用j++因为for循环i会加1
              if(j < s.length && g[i] <= s[j]){
    
    
                  count++;
                   j++;
                  continue;
              }
              //不符合的时候,只j动,i不动
              if(j < s.length && g[i] > s[j]){
    
    
                  j++;
                  i--;//因为for循环里面i会加1,所以我们就在这里先减1;
                  continue;
              }
              //当饼干分完的时候直接退出循环,更节省时间
              if(j == s.length){
    
    
                 return count;
              }                           
          }
          return count;
    }
}

Code simplification:

class Solution {
    
    
  public int findContentChildren(int[] g, int[] s) {
    
    
    if (g.length == 0 || s.length == 0) return 0;//特殊情况
    //同样要先排序
    Arrays.sort(g);
    Arrays.sort(s);
    //指向孩子数组的指针
    int i = 0;
    //指向饼干数组的指针
    int j = 0;
    //注意循环条件
    while (i < g.length && j < s.length) {
    
    
        //如果如何条件则两个指针都移动
        if (g[i] <= s[j]) {
    
    
            i++;
        }
        //不符合条件时只移动一个
        j++;
    }
    return i;//因为只有符合的时候i才会增加,所以i的值则为个数
  }
}

The efficiency of the simplified code is close to double one hundred, so you can try to write it.

to sum up


Insert picture description here

Guess you like

Origin blog.csdn.net/tan45du_yuan/article/details/109186120