当前位置:网站首页>Leetcode- distribute cookies - simple

Leetcode- distribute cookies - simple

2022-06-13 05:49:00 AnWenRen

title :455 Distribute cookies - Simple

subject

Suppose you are a great parent , Want to give your kids some cookies . however , Each child can only give one biscuit at most .

For every child i, All have an appetite value g[i], This is the smallest size of biscuit that can satisfy children's appetite ; And every cookie j, They all come in one size s[j] . If s[j] >= g[i], We can put this biscuit j Assign to children i , The child will be satisfied . Your goal is to meet as many children as possible , And output the maximum value .

Example 1

 Input : g = [1,2,3], s = [1,1]
 Output : 1
 explain : 
 You have three children and two biscuits ,3 The appetites of a child are :1,2,3.
 Although you have two biscuits , Because they are all of the same size 1, You can only make your appetite worth 1 The children of .
 So you should output 1.

Example 2

 Input : g = [1,2], s = [1,2,3]
 Output : 2
 explain : 
 You have two children and three biscuits ,2 The appetites of a child are 1,2.
 You have enough cookies and sizes to satisfy all children .
 So you should output 2

Tips

  • 1 <= g.length <= 3 * 104
  • 0 <= s.length <= 3 * 104
  • 1 <= g[i], s[j] <= 231 - 1

Code Java

int sum = 0;
Arrays.sort(g);
Arrays.sort(s);
int j;
int index = 0;
for (int i = 0; i < g.length && index < s.length; i++) {
    
    j = index;
    while (j < s.length){
    
        if (s[j] >= g[i]) {
    
            sum ++;
            break;
        }
        j++;
    }
    index = j + 1;
}
return sum;
原网站

版权声明
本文为[AnWenRen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280507175821.html