A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 455. Assign Cookies, the solution in this repository is mainly a greedy solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: greedy.
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are findContentChildren.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01/**
02Assume 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 gi, which is the minimum size of a cookie that the child will be content with; and each cookie j has a size sj. If sj >= gi, 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.
03
04Note:
05You may assume the greed factor is always positive.
06You cannot assign more than one cookie to one child.
07
08Example 1:
09Input: [1,2,3], [1,1]
10
11Output: 1
12
13Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3.
14And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.
15You need to output 1.
16Example 2:
17Input: [1,2], [1,2,3]
18
19Output: 2
20
21Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2.
22You have 3 cookies and their sizes are big enough to gratify all of the children,
23You need to output 2.
24**/
25
26//Runtime: 56 ms, faster than 20.91% of C++ online submissions for Assign Cookies.
27//Memory Usage: 10.2 MB, less than 100.00% of C++ online submissions for Assign Cookies.
28
29class Solution {
30public:
31 int findContentChildren(vector<int>& g, vector<int>& s) {
32 int cur1 = 0, cur2 = 0;
33 int ans = 0;
34
35 sort(g.begin(), g.end());
36 sort(s.begin(), s.end());
37
38 while(cur1 < g.size() && cur2 < s.size()){
39 if(s[cur2] >= g[cur1]){
40 ans++;
41 cur1++;
42 cur2++;
43 }else{
44 cur2++;
45 }
46 }
47
48 return ans;
49 }
50};
Cost