mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
200 lines
5.6 KiB
Python
200 lines
5.6 KiB
Python
{
|
|
"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": {},
|
|
"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": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# %load test_assign_cookie.py\n",
|
|
"import unittest\n",
|
|
"\n",
|
|
"\n",
|
|
"class TestAssignCookie(unittest.TestCase):\n",
|
|
"\n",
|
|
" def test_assign_cookie(self):\n",
|
|
" solution = Solution()\n",
|
|
" self.assertRaises(TypeError, solution.find_content_children, None, None)\n",
|
|
" self.assertEqual(solution.find_content_children([1, 2, 3], \n",
|
|
" [1, 1]), 1)\n",
|
|
" self.assertEqual(solution.find_content_children([1, 2], \n",
|
|
" [1, 2, 3]), 2)\n",
|
|
" self.assertEqual(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.7.2"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 1
|
|
}
|