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 9f1bd4e

Browse files
Coin Change
1 parent 6982190 commit 9f1bd4e

File tree

2 files changed

+71
-36
lines changed

2 files changed

+71
-36
lines changed
Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,67 @@
11
package com.java.coin.change;
22

3-
import java.util.Arrays;
3+
/*
4+
* All possible change for the given money
5+
* ---------------------------------------
6+
* say the bank has following denominations
7+
* 1, 2, 3
8+
*
9+
* Also the bank has unlimited coins of 1
10+
* unlimited coins of 2 and unlimited coins of 3.
11+
*
12+
* One customer comes to the bank to get the change for 5
13+
* What are all the ways the bank cashier can give the change?
14+
* need change for 5?
15+
* 1) 1, 1, 1, 1, 1
16+
* 2) 1, 1, 1, 2
17+
* 3) 1, 2, 2
18+
* 4) 1, 1, 3
19+
* 5) 2, 3
20+
* So there are 5 ways the cashier can give change.
21+
*
22+
*
23+
* another example coins are {5, 10, 20}
24+
* need change for 40?
25+
* All possible ways are
26+
* 1) 5, 5, 5, 5, 5, 5, 5, 5
27+
* 2) 5, 5, 5, 5, 5, 5, 10
28+
* 3) 5, 5, 5, 5, 10, 10
29+
* 4) 5, 5, 5, 5, 20
30+
* 5) 5, 5, 10, 10, 10
31+
* 6) 5, 5, 10, 20
32+
* 7) 10, 10, 10, 10
33+
* 8) 10, 10, 20
34+
* 9( 20, 20
35+
*
36+
* So total possible ways are 9.
37+
*/
438
public class MaxNoOfWays {
5-
639
public static void main(String[] args) {
7-
int n = 5;
8-
int coins[] = {1,2,5};
40+
41+
// int n = 5;
42+
// int coins[] = {1,2,3};
43+
44+
int n = 40;
45+
int coins[] = {5,10,20};
946
int noOfCoins = coins.length;
1047

1148
int table[] = new int[n+1];
1249
table[0] = 1;
1350
for(int i=0;i<noOfCoins;i++){
1451
for(int j=coins[i];j<=n;j++)
1552
table[j] += table[j-coins[i]];
16-
// System.out.println(i+" "+Arrays.toString(array));
1753
}
18-
System.out.println(table[n]);
1954
System.out.println("Maximum no of ways to get change is :: "+table[n]);
2055
}
21-
}
56+
}
57+
/*
58+
OUTPUT
59+
coins = {1,2,3}
60+
n = 5
61+
Maximum no of ways to get change is :: 5
62+
63+
OUTPUT
64+
coins = {5,10,20}
65+
n = 40
66+
Maximum no of ways to get change is :: 9
67+
*/
Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package com.java.coin.change;
22

3-
import java.util.Arrays;
4-
import java.util.Scanner;
5-
63
public class MinimumCoins {
74
public static void main(String[] args) {
8-
Scannerscanner = newScanner(System.in);
9-
int noOfCoins = Integer.parseInt(scanner.nextLine().trim());
10-
intcoins[] = newint[noOfCoins];
11-
for(int i=0;i<noOfCoins;i++)
12-
coins[i] = Integer.parseInt(scanner.nextLine().trim());
13-
int n = Integer.parseInt(scanner.nextLine().trim());
5+
// int n = 40;
6+
// int coins[] = {5,10,20};
7+
8+
int n = 5;
9+
intcoins[] = {1,2};
10+
int noOfCoins = coins.length;
1411

1512
int table[] = new int[n+1];
1613
table[0] = 0;
@@ -25,29 +22,21 @@ public static void main(String[] args) {
2522
if(sub_res != Integer.MAX_VALUE && sub_res+1 < table[i])
2623
table[i] = sub_res+1;
2724
}
28-
// System.out.println(i+" "+Arrays.toString(table));
2925
}
30-
System.out.print(table[n]);
31-
scanner.close();
26+
System.out.print(table[n]+" coins are enough to give change.");
3227
}
3328
}
3429
/*
35-
36-
Input
37-
4
38-
7
39-
3
40-
2
41-
6
42-
13
43-
Output
44-
2
45-
46-
Input
47-
3
48-
1
49-
2
50-
10
51-
5
30+
INPUT
31+
coins = {1,2}
32+
n = 5
33+
OUTPUT
34+
2 coins are enough to give change.
35+
36+
INPUT
37+
coins = {5,10,20}
38+
n = 40
39+
OUTPUT
40+
2 coins are enough to give change.
5241
5342
*/

0 commit comments

Comments
(0)

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