{ "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": [ "# 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", "
\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", "" ] }, { "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", " * Increment result counter\n", " * Increment the index to the greed factor\n", " * Careful of this index going out of bounds\n", "* Return the result counter\n", "\n", "Complexity:\n", "* Time: O(n log n) for the sort\n", "* Space: O(1), assuming the sort is in-place" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Code" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "class Solution(object):\n", "\n", " def find_content_children(self, greed_indices, cookie_sizes):\n", " if greed_indices is None or cookie_sizes is None:\n", " raise TypeError('greed_indices or cookie_sizes cannot be None')\n", " if not greed_indices or not cookie_sizes:\n", " return 0\n", " greed_indices.sort()\n", " cookie_sizes.sort()\n", " greed_index = 0\n", " num_children = 0\n", " for size in cookie_sizes:\n", " if greed_index >= len(greed_indices):\n", " break\n", " if size >= greed_indices[greed_index]:\n", " num_children += 1\n", " greed_index += 1\n", " return num_children" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Unit Test" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting test_assign_cookie.py\n" ] } ], "source": [ "%%writefile 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": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Success: test_find_content_children\n" ] } ], "source": [ "%run -i test_assign_cookie.py" ] } ], "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 }