Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1023737

Browse files
authored
Merge pull request fnplus#625 from Devamjoshi3/master
Added Make Change
2 parents f2b5302 + cb0ef8c commit 1023737

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
5+
/**
6+
* Given a list of coins of distinct denominations and total amount of money. Output the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
7+
* You may assume that there are infinite numbers of coins of each type.
8+
*
9+
* Input:
10+
* The first line contains 'T' denoting the number of testcases. Then follows description of testcases.
11+
* Each cases begins with the two space separated integers 'n' and 'amount' denoting the total number of distinct coins and total amount of money respectively.
12+
* The second line contains n space-separated integers A1, A2, ..., An denoting the values of coins.
13+
*
14+
* Output:
15+
* For each testcase, in a new line, print the minimum number of coins required to make up that amount, print -1 if it is impossible to make that amount using given coins.
16+
*
17+
* Constraints:
18+
* 1<=T<=100
19+
* 1<=n<=100
20+
* 1<=Ai<=1000
21+
* 1<=amount<=100000
22+
*
23+
* Example:
24+
* Input :
25+
* 2
26+
* 3 11
27+
* 1 2 5
28+
* 2 7
29+
* 2 6
30+
* Output :
31+
* 3
32+
* -1
33+
*/
34+
35+
36+
public class MakeChange {
37+
38+
public static void main(String[] args) throws IOException {
39+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
40+
41+
int t = Integer.parseInt(br.readLine().trim());
42+
43+
while ( t-- > 0){
44+
45+
String line = br.readLine();
46+
String[] strs = line.trim().split("\\s+");
47+
48+
int n = Integer.parseInt(strs[0]);
49+
int sum = Integer.parseInt(strs[1]);
50+
51+
line = br.readLine();
52+
strs = line.trim().split("\\s+");
53+
54+
int[] a = new int[n];
55+
56+
for (int i=0; i<n; i++){
57+
a[i] = Integer.parseInt(strs[i]);
58+
}
59+
60+
61+
System.out.println(minCoin(n, sum, a));
62+
63+
64+
65+
}
66+
67+
}
68+
69+
private static int minCoin(int n, int sum, int[] a) {
70+
71+
int[] lookup = new int[sum+1];
72+
73+
for(int i=0; i<sum+1; i++){
74+
lookup[i] = Integer.MAX_VALUE;
75+
}
76+
77+
lookup[0] = 0;
78+
79+
for (int i=1; i<sum+1; i++){
80+
minCoinUtil(i, lookup, a, n);
81+
}
82+
83+
return lookup[sum] == Integer.MAX_VALUE? -1:lookup[sum];
84+
}
85+
86+
private static void minCoinUtil(int k, int[] lookup, int[] a, int n) {
87+
88+
int min = Integer.MAX_VALUE;
89+
90+
for (int i=0; i<n; i++){
91+
if(k-a[i] < 0 || lookup[k-a[i]] == Integer.MAX_VALUE)
92+
continue;
93+
94+
if (1+lookup[k - a[i]] < min){
95+
min = 1+ lookup[k -a[i]];
96+
}
97+
}
98+
99+
lookup[k] = min;
100+
}
101+
102+
103+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
class MatrixChainMultiplication
2+
{
3+
// Function to find the most efficient way to multiply
4+
// given sequence of matrices
5+
public static int MatrixChainMultiplication(int[] dims, int i, int j)
6+
{
7+
// base case: one matrix
8+
if (j <= i + 1) {
9+
return 0;
10+
}
11+
12+
// stores minimum number of scalar multiplications (i.e., cost)
13+
// needed to compute the matrix M[i+1]...M[j] = M[i..j]
14+
int min = Integer.MAX_VALUE;
15+
16+
// take the minimum over each possible position at which the
17+
// sequence of matrices can be split
18+
19+
/*
20+
(M[i+1]) x (M[i+2]..................M[j])
21+
(M[i+1]M[i+2]) x (M[i+3.............M[j])
22+
...
23+
...
24+
(M[i+1]M[i+2]............M[j-1]) x (M[j])
25+
*/
26+
27+
for (int k = i + 1; k <= j - 1; k++)
28+
{
29+
// recur for M[i+1]..M[k] to get a i x k matrix
30+
int cost = MatrixChainMultiplication(dims, i, k);
31+
32+
// recur for M[k+1]..M[j] to get a k x j matrix
33+
cost += MatrixChainMultiplication(dims, k, j);
34+
35+
// cost to multiply two (i x k) and (k x j) matrix
36+
cost += dims[i] * dims[k] * dims[j];
37+
38+
if (cost < min) {
39+
min = cost;
40+
}
41+
}
42+
43+
// return min cost to multiply M[j+1]..M[j]
44+
return min;
45+
}
46+
47+
// main function
48+
public static void main(String[] args)
49+
{
50+
// Matrix M[i] has dimension dims[i-1] x dims[i] for i = 1..n
51+
// input is 10 ×ばつ 30 matrix, 30 ×ばつ 5 matrix, 5 ×ばつ 60 matrix
52+
int[] dims = { 10, 30, 5, 60 };
53+
54+
System.out.print("Minimum cost is " +
55+
MatrixChainMultiplication(dims, 0, dims.length - 1));
56+
}
57+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /