Leetcode 455. Assign Cookies

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

The Description of the problem

Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.

Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be content with; and each cookie j has a size s[j]. If s[j] >= s[i], we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.

The Slution 1 from small to large

class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        sort(g.begin(), g.end());
        sort(g.begin(), g.end());
        int i = j = 0;
        while (i < g.size() && j < s.size()) {
            if (s[i] >= g[i]) i++; // the essential operation, only if the current child 
             j++;                      is contnt, the next child can get the opportunity
        }
        return i;
    }
};

The analysis for above codes(Generated by ChatGPT):

The above code is a solution to the problem of "Assign Cookies" on LeetCode, which is given two arrays representing the sizes of children's greed factor (g) and the sizes of cookies (s), the goal is to find the maximum number of children who are content with the cookies they receive. The function takes two arguments, a vector of integers representing the sizes of the children's greed factor (g) and a vector of integers representing the sizes of the cookies (s).

The first step is to sort both arrays in ascending order using the built-in sort function. This is done to ensure that the smallest cookies are assigned to the children with the lowest greed factor. The next step is to initialize two variables, i and j, to 0. These variables will be used to iterate through the two arrays, g and s, respectively.

The function then enters a while loop that continues until either the end of the g array is reached or the end of the s array is reached. Inside the while loop, the function checks if the current cookie size (s[j]) is greater than or equal to the current child's greed factor (g[i]). If it is, the function increments the i variable, which moves on to the next child. The j variable is also incremented so that the next cookie can be checked. This step is also the essential operation that makes sure only the children with lower greed factor can get the opportunity to get the cookie.

The while loop continues until either the end of the g array is reached or the end of the s array is reached. At this point, the function returns the value of i, which represents the number of children who are content with the cookies they received.

The Solution 2 from large to small

class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        int res = 0;
        sort(g.begin(), g.end());
        sort(s.begin(), s.end());
        int n_g = g.size(), n_s = s.size();
        int s_index = n_s - 1;
        for (int g_index = n_g - 1; g_index >= 0; g_index--) {
            while (s_index >= 0 && s[s_index] >= g[g_index]) {
                s_index--;
                res++;
                break;
            }
        }
        return res;
    }
};

The analysis for above codes(Generated by ChatGPT):

The above code is a solution to the problem of "Assign Cookies" from LeetCode. It is a C++ implementation of the solution. The solution uses two main data structures: the vector g represents the children's greed factor and the vector s represents the size of the cookies. The problem is to find the maximum number of children that can be satisfied with the given cookies.

The first step in the solution is to sort the two vectors g and s in ascending order. This is done using the sort() function provided by the C++ STL. Sorting the vectors in this way ensures that we can compare the greed factor of the children with the sizes of the cookies in the correct order.

The next step is to initialize two variables, n_g and n_s, which represent the sizes of the vectors g and s respectively. The variable s_index is initialized to the last index of the vector s, and g_index is initialized to the last index of the vector g. The variable res is initialized to 0, which will be used to keep track of the number of children that can be satisfied.

The solution then enters a for loop, which starts at the last index of the vector g and decrements until it reaches the first index. In each iteration, the solution checks if the size of the cookie at the current index of s is greater than or equal to the greed factor of the child at the current index of g. If this is true, the s_index is decremented and res is incremented. This process continues until either all the children have been processed or all the cookies have been processed.

Finally, the function returns the value of res, which represents the maximum number of children that can be satisfied with the given cookies.

The solution has a time complexity of O(nlogn) because of the sorting step, and O(n) for the iterating through the arrays, thus overall it's O(nlogn).

Overall, the solution is relatively simple, easy to understand and efficient in terms of time complexity.

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6