interactive-coding-challenges/online_judges/assign_cookies/assign_cookies_challenge.ipynb

204 lines
5.7 KiB
Python
Raw Normal View History

2017-03-29 16:36:40 +08:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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": [
"# Challenge 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)\n",
"* [Solution Notebook](#Solution-Notebook)"
]
},
{
"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",
"Refer to the [Solution Notebook](). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class Solution(object):\n",
"\n",
" def find_content_children(self, g, s):\n",
" # TODO: Implement me\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**The following unit test is expected to fail until you solve the challenge.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# %load test_assign_cookie.py\n",
"from nose.tools import assert_equal, assert_raises\n",
"\n",
"\n",
"class TestAssignCookie(object):\n",
"\n",
" def test_assign_cookie(self):\n",
" solution = Solution()\n",
" assert_raises(TypeError, solution.find_content_children, None, None)\n",
" assert_equal(solution.find_content_children([1, 2, 3], \n",
" [1, 1]), 1)\n",
" assert_equal(solution.find_content_children([1, 2], \n",
" [1, 2, 3]), 2)\n",
" assert_equal(solution.find_content_children([7, 8, 9, 10], \n",
" [5, 6, 7, 8]), 2)\n",
" print('Success: test_find_content_children')\n",
"\n",
"\n",
"def main():\n",
" test = TestAssignCookie()\n",
" test.test_assign_cookie()\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solution Notebook\n",
"\n",
"Review the [Solution Notebook]() for a discussion on algorithms and code solutions."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}