Source : https://www.hackerrank.com/challenges/coin-change
You are working at the cash counter at a fun-fair, and you have different types of coins available to you in infinite quantities. The value of each coin is already given. Can you determine the number of ways of making change for a particular number of units using the given types of coins?
For example, if you have types of coins, and the value of each type is given as respectively, you can make change for units in three ways: , , and .
Function Description
Complete the getWays function in the editor below. It must return an integer denoting the number of ways to make change.
getWays has the following parameter(s):
- n: an integer, the amount to make change for
- c: an array of integers representing available denominations
Input Format
The first line contains two space-separated integers describing the respective values of and , where:
is the number of units
is the number of coin types
The second line contains space-separated integers describing the respective values of each coin type : (the list of distinct coins available in infinite amounts).
Constraints
- Each is guaranteed to be distinct.
Hints
Solve overlapping subproblems using Dynamic Programming (DP):
You can solve this problem recursively but will not pass all the test cases without optimizing to eliminate the overlapping subproblems. Think of a way to store and reference previously computed solutions to avoid solving the same subproblem multiple times.* Consider the degenerate cases:
- How many ways can you make change for cents? - How many ways can you make change for cents if you have no coins?* If you're having trouble defining your solutions store, then think about it in terms of the base case .- The answer may be larger than a -bit integer.
Output Format
Print a long integer denoting the number of ways we can get a sum of from the given infinite supply of types of coins.
Sample Input 0
4 31 2 3
Sample Output 0
4
Explanation 0
There are four ways to make change for using coins with values given by :
Thus, we print as our answer.
Sample Input 1
10 42 5 3 6
Sample Output 1
5
Explanation 1
There are five ways to make change for units using coins with values given by :
Thus, we print as our answer.
Source : https://www.hackerrank.com/challenges/coin-change
Solution
// Karthikalapati.blogspot.com | |
import java.util.Scanner; | |
import java.util.HashMap; | |
// Use a HashMap as a cache to speed up runtime | |
// Must use "long" instead of "int" to avoid integer overflow | |
public class Solution { | |
public static void main(String[] args) { | |
/* Save input */ | |
Scanner scan = new Scanner(System.in); | |
int n = scan.nextInt(); | |
int m = scan.nextInt(); | |
int [] coins = new int[m]; | |
for (int i = 0; i < m; i++) { | |
coins[i] = scan.nextInt(); | |
} | |
scan.close(); | |
System.out.println(numWays(n, coins)); | |
} | |
public static long numWays(int n, int [] coins) { | |
if (n < 0) { | |
return 0; | |
} | |
return numWays(n, coins, 0, new HashMap<String, Long>()); | |
} | |
public static long numWays(int n, int [] coins, int coinNumber, HashMap<String, Long> cache) { | |
/* Check our cache */ | |
String key = n + "," + coinNumber; | |
if (cache.containsKey(key)) { | |
return cache.get(key); | |
} | |
/* Base case */ | |
if (coinNumber == coins.length - 1) { | |
if (n % coins[coinNumber] == 0) { | |
cache.put(key, 1L); | |
return 1; | |
} else { | |
cache.put(key, 0L); | |
return 0; | |
} | |
} | |
/* Recursive case */ | |
long ways = 0; | |
for (int i = 0; i <= n; i += coins[coinNumber]) { | |
ways += numWays(n - i, coins, coinNumber + 1, cache); | |
} | |
/* Cache and return solution */ | |
cache.put(key, ways); | |
return ways; | |
} | |
} |
No comments:
Post a Comment