{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy\n", "def changeDp(money, coins):\n", " D = numpy.zeros((len(coins), (money + 1)), dtype = int)\n", " # Initialize first row of the matrix\n", " D[0,1:] = range(1, money + 1)\n", " for i in range(1, len(coins)):\n", " for j in range(0, money + 1):\n", " if j < coins[i]:\n", " D[i,j] = D[i-1,j]\n", " else:\n", " D[i,j] = min(D[i-1,j],\n", " D[i,j - coins[i]] + 1)\n", " return D[-1][-1]" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "changeDp(12, [1,4,5])" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "coins = [50,25,20,10,5,1][::-1]\n", "changeDp(40, coins)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "735" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "coins = [1,3,5,11,20,21,22]\n", "changeDp(16164, coins)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy\n", "def changeDp(money, coins):\n", " D = numpy.zeros((len(coins), (money + 1)), dtype = int)\n", " # Initialize first row of the matrix\n", " D[0,1:] = range(1, money + 1)\n", " for i in range(1, len(coins)):\n", " for j in range(0, money + 1):\n", " if j < coins[i]:\n", " D[i,j] = D[i-1,j]\n", " else:\n", " D[i,j] = min(D[i-1,j],\n", " D[i,j - coins[i]] + 1)\n", " return D" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n", " 17, 18, 19, 20],\n", " [ 0, 1, 2, 1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6, 5, 6,\n", " 7, 6, 7, 8]])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "changeDp(20, [2,3])" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def DPChange(money,coins):\n", " MinNumCoins = {}\n", " MinNumCoins[0] = 0\n", " for m in range(1,money+1):\n", " MinNumCoins[m] = float('inf')\n", " for i in range(len(coins)):\n", " if m >= coins[i]:\n", " if MinNumCoins[m-coins[i]] + 1 < MinNumCoins[m]:\n", " MinNumCoins[m] = MinNumCoins[m-coins[i]] + 1\n", " return MinNumCoins" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0: 0,\n", " 1: inf,\n", " 2: 1,\n", " 3: 1,\n", " 4: 2,\n", " 5: 2,\n", " 6: 2,\n", " 7: 3,\n", " 8: 3,\n", " 9: 3,\n", " 10: 4,\n", " 11: 4,\n", " 12: 4,\n", " 13: 5,\n", " 14: 5,\n", " 15: 5,\n", " 16: 6,\n", " 17: 6,\n", " 18: 6,\n", " 19: 7,\n", " 20: 7,\n", " 21: 7,\n", " 22: 8,\n", " 23: 8,\n", " 24: 8}" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DPChange(20, [2,3])" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.13" } }, "nbformat": 4, "nbformat_minor": 1 }