当前位置:网站首页>Leetcode (455) - distribute cookies

Leetcode (455) - distribute cookies

2022-06-24 20:33:00 SmileGuy17

Leetcode(455)—— Distribute cookies

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 1 1 <= g.length <= 3 ∗ 1 0 4 3 * 10^4 3104
  • 0 0 0 <= s.length <= 3 ∗ 1 0 4 3 * 10^4 3104
  • 1 1 1 <= g[i], s[j] <= 2 31 − 1 2^{31 - 1} 2311

Answer key

Key points : Think about the local optimum , Figure out the global optimum , It feels that the local optimum can deduce the global optimum , And can't think of a counterexample , Then try greed

Method 1 : Sort + greedy

Ideas

​​   Because children with the least hunger are the most likely to be full , So let's think about this kid first . In order to try to make the rest of the biscuits satisfy the hungry children , So we should put more than or equal to this child's hunger degree 、 And the smallest biscuit for the child . After satisfying the child , We take the same strategy , Consider the least hungry child left , Until there are no biscuits that meet the conditions .

​​   In order to meet as many children as possible , From the perspective of greed , Each child should be satisfied in the order of their appetite from small to large , And for every child , Choose the smallest biscuit that meets the child's appetite . Prove the following .

​​   Suppose there is m m m A child , The appetites are g 1 g_1 g1 To g m g_m gm, Yes n n n A biscuit , The dimensions are s 1 s_1 s1 To s n s_n sn, Satisfy g i ≤ g i + 1 g_i \le g_{i+1} gigi+1 and s j ≤ s j + 1 s_j \le s_{j+1} sjsj+1, among 1 ≤ i < m 1 \le i < m 1i<m, 1 ≤ j < n 1 \le j < n 1j<n.

Assume before i − 1 i-1 i1 After the first child distributed the biscuits , It can meet the requirements of i i i The smallest biscuit for a child's appetite is the j j j A biscuit , namely s j s_j sj Is the rest of the biscuits to meet g i ≤ s j g_i \le s_j gisj The minimum value of , The optimal solution is to put the j j j A biscuit is allocated to the i i i A child . If not allocated in this way , Consider the following two scenarios :

  • If i < m i<m i<m And g i + 1 ≤ s j g_{i+1} \le s_j gi+1sj It was also established , Then if j j j A biscuit is allocated to the i + 1 i+1 i+1 A child , And there are still some biscuits left , Then you can put the second j + 1 j+1 j+1 A biscuit is allocated to the i i i A child , The result of distribution will not satisfy more children ;
  • If j < n j<n j<n, Then if j + 1 j+1 j+1 A biscuit is allocated to the i i i A child , When g i + 1 ≤ s j g_{i+1} \le s_j gi+1sj when , You can put the second j j j A biscuit is allocated to the i + 1 i+1 i+1 A child , The result of distribution will not satisfy more children ; When g i + 1 > s j g_{i+1}>s_j gi+1>sj when , The first j j j A biscuit cannot be assigned to any child , So there is one less available biscuit left , Therefore, the result of distribution will not allow more children to be satisfied , Even fewer children may be satisfied because of the lack of a usable biscuit .

​​   Based on the above analysis , You can use greedy methods to satisfy as many children as possible .

​​   First, the array g g g and s s s Sort , And then traverse from small to large g g g Every element in , For each element, find a that satisfies that element s s s The smallest element in . To be specific , Make i i i yes g g g The subscript , j j j yes s s s The subscript , At the beginning i i i and j j j All for 0 0 0, Do the following .

​​   For each element g [ i ] g[i] g[i], find Unused The smallest j j j bring g [ i ] ≤ s [ j ] g[i] \le s[j] g[i]s[j], be s [ j ] s[j] s[j] You can meet g [ i ] g[i] g[i]. because g g g and s s s It's in order , Therefore, the whole process only needs to compare the array g g g and s s s Traverse once each . When one of the two arrays ends , It means that all the children are assigned to biscuits , Or all the cookies have been distributed or tried to be distributed ( Maybe some cookies can't be distributed to any children ), At this time, the number of children assigned to biscuits is the maximum number that can be met .

Code implementation

My own :

class Solution {
    
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
    
        //  Ascending sort 
        sort(g.begin(), g.end());
        sort(s.begin(), s.end());

        int n = 0;
        vector<int>::iterator p = s.begin();

        for(auto kid: g){
    
            while(p != s.end()){
    
                //  Judge whether this biscuit can make the child full 
                if(kid <= *p){
    
                    n++;
                    p++;
                    break;
                }else p++;
            }
            if(p == s.end()) break;
        }
        return n;
    }
};

Complexity analysis

Time complexity O ( m l o g m + n l o g n ) O(mlogm+nlogn) O(mlogm+nlogn), among m m m and n n n They are arrays g g g and s s s The length of . The time complexity of sorting two arrays is O ( m log ⁡ m + n log ⁡ n ) O(m \log m + n \log n) O(mlogm+nlogn), The time complexity of traversing an array is O ( m + n ) O(m+n) O(m+n), So the total time complexity is O ( m log ⁡ m + n log ⁡ n ) O(m \log m + n \log n) O(mlogm+nlogn).
Spatial complexity O ( log ⁡ m + log ⁡ n ) O(\log m + \log n) O(logm+logn), among m m m and n n n They are arrays g g g and s s s The length of . The space complexity is mainly the extra space cost of sorting .

原网站

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