Skip to content

Commit 635dff6

Browse files
committed
feat(algorithms, greedy): assign cookies
1 parent 1e5bdc7 commit 635dff6

5 files changed

Lines changed: 129 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Assign Cookies
2+
3+
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most
4+
one cookie.
5+
6+
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
7+
each cookie j has a size s[j]. If s[j] >= g[i], we can assign the cookie j to the child i, and the child i will be content.
8+
Your goal is to maximize the number of your content children and output the maximum number.
9+
10+
## Constraints
11+
12+
- 1 <= greed.length <= 3 * 104
13+
- 0 <= cookies.length <= 3 * 104
14+
- 1 <= greed[i], cookies[j] <= 231 - 1
15+
16+
## Examples
17+
18+
Example 1:
19+
```text
20+
Input: g = [1,2,3], s = [1,1]
21+
Output: 1
22+
Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3.
23+
And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1
24+
content.
25+
You need to output 1.
26+
```
27+
28+
Example 2:
29+
```text
30+
Input: g = [1,2], s = [1,2,3]
31+
Output: 2
32+
Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2.
33+
You have 3 cookies and their sizes are big enough to gratify all of the children,
34+
You need to output 2.
35+
```
36+
37+
## Topics
38+
39+
- Array
40+
- Two Pointers
41+
- Greedy
42+
- Sorting
43+
44+
## Solution
45+
46+
Intiutively, we want to give each child the smallest cookie that satisfies them. This allows us to save the larger
47+
cookies for the greedier children and allows us to maximize the number of satisfied children.
48+
49+
The greedy algorithm sorts both the greeds and cookies arrays in ascending order. This places the child with the smallest
50+
greed and the smallest cookie at the front of each array.
51+
52+
For example:
53+
54+
```text
55+
greeds = [1, 3, 3, 4]
56+
cookies = [2, 2, 3, 4]
57+
```
58+
59+
We then initialize two pointers `i` and `j` to the start of the `greeds` and `cookies` arrays, respectively. `i`
60+
represents the current child and `j` represents the current cookie.
61+
62+
If `cookies[j] >= greeds[i]`, that means the current cookie can satisfy the current child. We increment the number of
63+
satisfied children and move to the next child and cookie.
64+
65+
![Solution 1](./images/solutions/assign_cookies_solution_1.png)
66+
67+
If `cookies[j] < greeds[i]`, that means the current cookie cannot satisfy the current child, so we move to the next cookie
68+
to see if it can.
69+
70+
![Solution 2](./images/solutions/assign_cookies_solution_2.png)
71+
72+
We can continue this process until we reach the end of either the greeds or cookies arrays, and return the number of
73+
satisfied children as the result.
74+
75+
### Complexity Analysis
76+
77+
#### Time Complexity
78+
79+
O(n log n + m log m) where n is the number of children and m is the number of cookies. We sort the
80+
greeds and cookies arrays in O(n log n + m log m) time, and then iterate through the arrays in O(n + m) time.
81+
82+
#### Space Complexity
83+
84+
O(1) We only use a constant amount of space for variables.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List
2+
3+
4+
def find_content_children(greeds: List[int], cookies: List[int]) -> int:
5+
# This in-place sorting of both g and s results in a time complexity of O(n log(n) + m log(m))
6+
greeds.sort()
7+
cookies.sort()
8+
9+
cookie, greed = 0, 0
10+
count = 0
11+
12+
# We iterate through each greed factor and cookie
13+
while greed < len(greeds) and cookie < len(cookies):
14+
# When we get a cookie that satisfies kid i, we assign that cookie to the child
15+
# and move along, increasing the count as well
16+
if cookies[cookie] >= greeds[greed]:
17+
count += 1
18+
greed += 1
19+
cookie += 1
20+
21+
return count
83 KB
Loading
45.2 KB
Loading
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import unittest
2+
from typing import List
3+
from parameterized import parameterized
4+
from algorithms.greedy.assign_cookies import find_content_children
5+
6+
7+
ASSIGN_COOKIES_TEST_CASES = [
8+
([1, 2, 3], [1, 1], 1),
9+
([1, 2], [1, 2, 3], 2),
10+
([10, 9, 8, 7], [5, 6, 7, 8], 2),
11+
]
12+
13+
14+
class AssignCookiesTestCase(unittest.TestCase):
15+
@parameterized.expand(ASSIGN_COOKIES_TEST_CASES)
16+
def test_find_content_children(
17+
self, greed: List[int], cookies: List[int], expected: int
18+
):
19+
actual = find_content_children(greed, cookies)
20+
self.assertEqual(expected, actual)
21+
22+
23+
if __name__ == "__main__":
24+
unittest.main()

0 commit comments

Comments
 (0)