"This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"# Solution Notebook"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Problem: Assign Cookies.\n",
"\n",
"See the [LeetCode](https://leetcode.com/problems/assign-cookies/) problem page.\n",
"\n",
"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 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.\n",
"\n",
"Note:\n",
"You may assume the greed factor is always positive. \n",
"You cannot assign more than one cookie to one child.\n",
"\n",
"Example 1:\n",
"Input: [1,2,3], [1,1]\n",
"\n",
"Output: 1\n",
"\n",
"Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3. \n",
"And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.\n",
"You need to output 1.\n",
"Example 2:\n",
"Input: [1,2], [1,2,3]\n",
"\n",
"Output: 2\n",
"\n",
"Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2. \n",
"You have 3 cookies and their sizes are big enough to gratify all of the children, \n",
"You need to output 2.\n",
"\n",
"* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Constraints\n",
"\n",
"* Are the inputs two list(int), one for greed factor and the other for cookie size?\n",
" * Yes\n",
"* Are the inputs are sorted increasing order?\n",
" * No\n",
"* Can we change inputs themselves, or do we need to make a copy?\n",
" * You can change them\n",
"* Is the output an int?\n",
" * Yes\n",
"* Is the greed factor always >= 1?\n",
" * Yes\n",
"* Can we assume the inputs are valid?\n",
" * No, check for None\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Test Cases\n",
"\n",
"<pre>\n",
"* None input -> TypeError\n",
"[1, 2, 3], [1, 1] -> 1\n",
"[1, 2], [1, 2, 3] -> 2\n",
"[7, 8, 9, 10], [5, 6, 7, 8] -> 2\n",
"</pre>"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Algorithm\n",
"\n",
"* Sort the inputs\n",
"* We'll keep an index to the current greed factor\n",
"* For each cookie\n",
" * Assign it to a child if its size >= the child's greed factor\n",