-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoinChange.java
More file actions
executable file
·49 lines (48 loc) · 1.44 KB
/
Copy pathCoinChange.java
File metadata and controls
executable file
·49 lines (48 loc) · 1.44 KB
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
40
41
42
43
44
45
46
47
48
49
import java.io.*;
import java.util.*;
import java.math.*;
class CoinChange
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int target = in.nextInt();
int numberOfCoins = in.nextInt();
int[] coinValue = new int[numberOfCoins];
long[][] coinSolutions = new long[numberOfCoins][target + 1];
for(int i = 0; i < numberOfCoins; i++)
{
coinValue[i] = in.nextInt();
coinSolutions[i][0] = 1;
}
for(int i = 0; i < numberOfCoins; i++)
{
for(int j = 1; j <= target; j ++)
{
if (i == 0)
{
if(coinValue[i] > j)
{
coinSolutions[i][j] = 0;
}
else
{
coinSolutions[i][j] = coinSolutions[i][j - coinValue[i]];
}
}
else
{
if(coinValue[i] > j)
{
coinSolutions[i][j] = coinSolutions[i - 1][j];
}
else
{
coinSolutions[i][j] = coinSolutions[i - 1][j] + coinSolutions[i][j - coinValue[i]];
}
}
}
}
System.out.println(coinSolutions[numberOfCoins - 1][target]);
}
}