File size: 1,105 Bytes
9438a57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.*;
/**
 * Making Change
 * change
 * 
 * 
 * Input:
 *     coins: A list of positive ints representing coin denominations
 *     total: An int value to make change for
 * 
 * Output:
 *     The number of distinct ways to make change adding up to total using only coins of the given values.
 *     For example, there are exactly four distinct ways to make change for the value 11 using coins [1, 5, 10, 25]:
 *         1. {1: 11, 5: 0, 10: 0, 25: 0}
 *         2. {1: 6, 5: 1, 10: 0, 25: 0}
 *         3. {1: 1, 5: 2, 10: 0, 25: 0}
 *         4. {1: 1, 5: 0, 10: 1, 25: 0}
 * 
 * Example:
 *     >>> possible_change([1, 5, 10, 25], 11)
 *     4
 *
 * @author derricklin
 */
public class POSSIBLE_CHANGE {
    public static int possible_change(int[] coins, int total) {
        if (total == 0) {
            return 1;
        }
        if (total < 0 ||coins.length==0) {
            return 0;
        }

        int first = coins[0];
        int[] rest = Arrays.copyOfRange(coins, 1, coins.length);
        return possible_change(coins, total-first) + possible_change(rest, total);
    }
}