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
http://yuanhsh.iteye.com/blog/2178328
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
这题和Coin Change很像,只不过这题需要考虑顺序的问题。
Solution1:
用递归做。就像斐波那契数列那样。
- public int atm(int x) {
- if (x <= 0)
- return x == 0 ? 1 : 0;
- return atm(x - 1) + atm(x - 5) + atm(x - 20) + atm(x - 100);
- }
但是这样很多重复的计算,输入太大的话会栈溢出。所以我们可以考虑把中间结果保存起来。
- public long tellMoneyCombinations(int money) {
- long[] f = new long[money+1];
- f[0] = 1;
- for(int i=1; i<=money; i++) {
- f[i] = f[i-1];
- if(i>=5) f[i]+=f[i-5];
- if(i>=20) f[i]+=f[i-20];
- if(i>=50) f[i]+=f[i-50];
- }
- return f[money];
- }
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 and (now with the restriction that , our solutions can be constructed in non-decreasing order), the set of solutions for this problem, , can be partitioned into two sets:
- There are those sets that do not contain any and
- Those sets that contain at least 1
If a solution does not contain , then we can solve the subproblem of with , or the solutions of .
If a solution does contain , then we are using at least one , thus we are now solving the subproblem of , . This is .
Thus, we can formulate the following:
with the base cases:
- (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)
- (no solution -- negative sum of money)
- (no solution -- we have money, but no change available)
- public int coinChange(int[] S, int N) {
- int[] f = new int[N+1];
- f[0] = 1;
- for(int i=0; i<S.length; i++) {
- for(int j=S[i]; j<=N; j++) {
- f[j] += f[j-S[i]];
- }
- }
- return f[N];
- }
- // Returns the count of ways we can sum S[0...m-1] coins to get sum n
- int count( int S[], int m, int n ) {
- // If n is 0 then there is 1 solution (do not include any coin)
- if (n == 0)
- return 1;
- // If n is less than 0 then no solution exists
- if (n < 0)
- return 0;
- // If there are no coins and n is greater than 0, then no solution exist
- if (m <=0 && n >= 1)
- return 0;
- // count is sum of solutions (i) including S[m-1] (ii) excluding S[m-1]
- return count( S, m - 1, n ) + count( S, m, n-S[m-1] );
- }
- int count( int S[], int m, int n )
- {
- int i, j, x, y;
- // We need n+1 rows as the table is consturcted in bottom up manner using
- // the base case 0 value case (n = 0)
- int table[n+1][m];
- // Fill the enteries for 0 value case (n = 0)
- for (i=0; i<m; i++)
- table[0][i] = 1;
- // Fill rest of the table enteries in bottom up manner
- for (i = 1; i < n+1; i++)
- {
- for (j = 0; j < m; j++)
- {
- // Count of solutions including S[j]
- x = (i-S[j] >= 0)? table[i - S[j]][j]: 0;
- // Count of solutions excluding S[j]
- y = (j >= 1)? table[i][j-1]: 0;
- // total count
- table[i][j] = x + y;
- }
- }
- return table[n][m-1];
- }
Time Complexity: O(mn)
Following is a simplified version of method 2. The auxiliary space required here is O(n) only.
- int count( int S[], int m, int n )
- {
- // table[i] will be storing the number of solutions for
- // value i. We need n+1 rows as the table is consturcted
- // in bottom up manner using the base case (n = 0)
- int table[n+1];
- // Initialize all table values as 0
- memset(table, 0, sizeof(table));
- // Base case (If given value is 0)
- table[0] = 1;
- // Pick all coins one by one and update the table[] values
- // after the index greater than or equal to the value of the
- // picked coin
- for(int i=0; i<m; i++)
- for(int j=S[i]; j<=n; j++)
- table[j] += table[j-S[i]];
- return table[n];
- }