## Sunday, November 29, 2015

### Number of unique ways that ATM can tender - 我的博客 - ITeye技术网站

Number of unique ways that ATM can tender - 我的博客 - ITeye技术网站
An atm can only dispense values of $1,$5, $20, and$50. Return the number
of unique ways that a $amount of X can be tendered. ($1, $5) is distinct from ($5, \$1)

Input: 4 Output: 1
Input: 6 Output: 3
Input: 100 Output: 954515231698

Solution1:

1. public int atm(int x) {
2.     if (x <= 0)
3.         return x == 0 ? 1 : 0;
4.      return atm(x - 1) + atm(x - 5) + atm(x - 20) + atm(x - 100);
5. }

1. public long tellMoneyCombinations(int money) {
2.     long[] f = new long[money+1];
3.     f[0] = 1;
4.     for(int i=1; i<=money; i++) {
5.         f[i] = f[i-1];
6.         if(i>=5) f[i]+=f[i-5];
7.         if(i>=20) f[i]+=f[i-20];
8.         if(i>=50) f[i]+=f[i-50];
9.     }
10.     return f[money];
11. }
http://yuanhsh.iteye.com/blog/2178328
Given a value N, if we want to make change for N cents, and we have infinite supply of each of S = { S1, S2, .. , Sm} valued coins, how many ways can we make the change? The order of coins doesn’t matter.
For example, for N = 4 and S = {1,2,3}, there are four solutions: {1,1,1,1},{1,1,2},{2,2},{1,3}. So output should be 4. For N = 10 and S = {2, 5, 3, 6}, there are five solutions: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. So the output should be 5.

We are trying to count the number of distinct sets.
Since order does not matter, we will impose that our solutions (sets) are all sorted in non-decreasing order (Thus, we are looking at sorted-set solutions: collections).
For a particular $N$ and $S=\{S_{1},S_{2},\ldots ,S_{m}\}$ (now with the restriction that $S_{1}, our solutions can be constructed in non-decreasing order), the set of solutions for this problem, $C(N,m)$, can be partitioned into two sets:
• There are those sets that do not contain any $S_{m}$ and
• Those sets that contain at least 1 $S_{m}$
If a solution does not contain $S_{m}$, then we can solve the subproblem of $N$ with $S=\{S_{1},S_{2},\ldots ,S_{{m-1}}\}$, or the solutions of $C(N,m-1)$.
If a solution does contain $S_{m}$, then we are using at least one $S_{m}$, thus we are now solving the subproblem of $N-S_{m}$$S=\{S_{1},S_{2},\ldots ,S_{m}\}$. This is $C(N-S_{m},m)$.
Thus, we can formulate the following:
$C(N,m)=C(N,m-1)+C(N-S_{m},m)$
with the base cases:
• $C(N,m)=1,N=0$ (one solution -- we have no money, exactly one way to solve the problem - by choosing no coin change, or, more precisely, to choose coin change of 0)
• $C(N,m)=0,N<0$ (no solution -- negative sum of money)
• $C(N,m)=0,N\geq 1,m\leq 0$ (no solution -- we have money, but no change available)
1. public int coinChange(int[] S, int N) {
2.     int[] f = new int[N+1];
3.     f[0] = 1;
4.     for(int i=0; i<S.length; i++) {
5.         for(int j=S[i]; j<=N; j++) {
6.             f[j] += f[j-S[i]];
7.         }
8.     }
9.     return f[N];
10. }

1. // Returns the count of ways we can sum  S[0...m-1] coins to get sum n
2. int count( int S[], int m, int n ) {
3.     // If n is 0 then there is 1 solution (do not include any coin)
4.     if (n == 0)
5.         return 1;
6.
7.     // If n is less than 0 then no solution exists
8.     if (n < 0)
9.         return 0;
10.
11.     // If there are no coins and n is greater than 0, then no solution exist
12.     if (m <=0 && n >= 1)
13.         return 0;
14.
15.     // count is sum of solutions (i) including S[m-1] (ii) excluding S[m-1]
16.     return count( S, m - 1, n ) + count( S, m, n-S[m-1] );

1. int count( int S[], int m, int n )
2. {
3.     int i, j, x, y;
4.
5.     // We need n+1 rows as the table is consturcted in bottom up manner using
6.     // the base case 0 value case (n = 0)
7.     int table[n+1][m];
8.
9.     // Fill the enteries for 0 value case (n = 0)
10.     for (i=0; i<m; i++)
11.         table[0][i] = 1;
12.
13.     // Fill rest of the table enteries in bottom up manner
14.     for (i = 1; i < n+1; i++)
15.     {
16.         for (j = 0; j < m; j++)
17.         {
18.             // Count of solutions including S[j]
19.             x = (i-S[j] >= 0)? table[i - S[j]][j]: 0;
20.
21.             // Count of solutions excluding S[j]
22.             y = (j >= 1)? table[i][j-1]: 0;
23.
24.             // total count
25.             table[i][j] = x + y;
26.         }
27.     }
28.     return table[n][m-1];
29. }
Time Complexity: O(mn)
Following is a simplified version of method 2. The auxiliary space required here is O(n) only.
1. int count( int S[], int m, int n )
2. {
3.     // table[i] will be storing the number of solutions for
4.     // value i. We need n+1 rows as the table is consturcted
5.     // in bottom up manner using the base case (n = 0)
6.     int table[n+1];
7.
8.     // Initialize all table values as 0
9.     memset(table, 0, sizeof(table));
10.
11.     // Base case (If given value is 0)
12.     table[0] = 1;
13.
14.     // Pick all coins one by one and update the table[] values
15.     // after the index greater than or equal to the value of the
16.     // picked coin
17.     for(int i=0; i<m; i++)
18.         for(int j=S[i]; j<=n; j++)
19.             table[j] += table[j-S[i]];
20.
21.     return table[n];
22. }