Skip to content

Commit f0d13c9

Browse files
committed
Added Coin Change
1 parent f41c7a4 commit f0d13c9

File tree

1 file changed

+185
-0
lines changed

1 file changed

+185
-0
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
---
2+
id: coin-change-problem
3+
title: Coin Change Problem using Dynamic Programming
4+
sidebar_label: Coin Change Problem
5+
tags: [python, java, c++, javascript, programming, algorithms, dynamic programming, tutorial, in-depth]
6+
description: In this tutorial, we will learn about the Coin Change Problem and its solution using Dynamic Programming in Python, Java, C++, and JavaScript with detailed explanations and examples.
7+
---
8+
9+
# Coin Change Problem using Dynamic Programming
10+
11+
The Coin Change Problem is a classic algorithmic problem that aims to find the number of ways to make change for a given amount using a specific set of coin denominations.
12+
13+
## Problem Statement
14+
15+
Given a set of coins with different denominations and a total amount of money, determine the number of unique ways to make change for the given amount using any combination of coins.
16+
17+
### Intuition
18+
19+
The problem can be efficiently solved using dynamic programming. By breaking down the problem into smaller subproblems and storing the results, dynamic programming helps avoid redundant calculations.
20+
21+
## Dynamic Programming Approach
22+
23+
Using dynamic programming, we build a table `dp` where `dp[i][j]` represents the number of ways to make change for amount `j` using the first `i` coins.
24+
25+
## Pseudocode for Coin Change Problem using DP
26+
27+
#### Initialize:
28+
29+
```markdown
30+
dp[0][j] = 1 // Base case: one way to make change for amount 0
31+
dp[i][0] = 1 // Base case: one way to make change for any amount using 0 coins
32+
33+
for i from 1 to n:
34+
for j from 1 to amount:
35+
if coins[i-1] > j:
36+
dp[i][j] = dp[i-1][j]
37+
else:
38+
dp[i][j] = dp[i-1][j] + dp[i][j-coins[i-1]]
39+
40+
return dp[n][amount]
41+
```
42+
43+
### Example Output:
44+
45+
Given the coins `[1, 2, 5]` and the amount `amount = 5`, the number of ways to make change is `4`.
46+
47+
### Output Explanation:
48+
49+
- There are four ways to make change for the amount `5` using the coins `[1, 2, 5]`:
50+
- `[1, 1, 1, 1, 1]`
51+
- `[1, 1, 1, 2]`
52+
- `[1, 2, 2]`
53+
- `[5]`
54+
55+
## Implementing Coin Change using DP
56+
57+
### Python Implementation
58+
59+
```python
60+
def coin_change(coins, amount):
61+
n = len(coins)
62+
dp = [[0] * (amount + 1) for _ in range(n + 1)]
63+
for i in range(n + 1):
64+
dp[i][0] = 1
65+
66+
for i in range(1, n + 1):
67+
for j in range(1, amount + 1):
68+
if coins[i - 1] > j:
69+
dp[i][j] = dp[i - 1][j]
70+
else:
71+
dp[i][j] = dp[i - 1][j] + dp[i][j - coins[i - 1]]
72+
73+
return dp[n][amount]
74+
75+
coins = [1, 2, 5]
76+
amount = 5
77+
print("Number of ways to make change:", coin_change(coins, amount))
78+
79+
```
80+
81+
### Java Implementation
82+
83+
```java
84+
public class CoinChange {
85+
public static int coinChange(int[] coins, int amount) {
86+
int n = coins.length;
87+
int[][] dp = new int[n + 1][amount + 1];
88+
for (int i = 0; i <= n; i++) {
89+
dp[i][0] = 1;
90+
}
91+
92+
for (int i = 1; i <= n; i++) {
93+
for (int j = 1; j <= amount; j++) {
94+
if (coins[i - 1] > j) {
95+
dp[i][j] = dp[i - 1][j];
96+
} else {
97+
dp[i][j] = dp[i - 1][j] + dp[i][j - coins[i - 1]];
98+
}
99+
}
100+
}
101+
102+
return dp[n][amount];
103+
}
104+
105+
public static void main(String[] args) {
106+
int[] coins = {1, 2, 5};
107+
int amount = 5;
108+
System.out.println("Number of ways to make change: " + coinChange(coins, amount));
109+
}
110+
}
111+
112+
```
113+
### C++ Implementation
114+
115+
```cpp
116+
#include <iostream>
117+
#include <vector>
118+
using namespace std;
119+
120+
int coinChange(vector<int>& coins, int amount) {
121+
int n = coins.size();
122+
vector<vector<int>> dp(n + 1, vector<int>(amount + 1, 0));
123+
for (int i = 0; i <= n; i++) {
124+
dp[i][0] = 1;
125+
}
126+
127+
for (int i = 1; i <= n; i++) {
128+
for (int j = 1; j <= amount; j++) {
129+
if (coins[i - 1] > j) {
130+
dp[i][j] = dp[i - 1][j];
131+
} else {
132+
dp[i][j] = dp[i - 1][j] + dp[i][j - coins[i - 1]];
133+
}
134+
}
135+
}
136+
137+
return dp[n][amount];
138+
}
139+
140+
int main() {
141+
vector<int> coins = {1, 2, 5};
142+
int amount = 5;
143+
cout << "Number of ways to make change: " << coinChange(coins, amount) << endl;
144+
return 0;
145+
}
146+
147+
```
148+
149+
### JavaScript Implementation
150+
151+
```javascript
152+
function coinChange(coins, amount) {
153+
let n = coins.length;
154+
let dp = new Array(n + 1).fill(0).map(() => new Array(amount + 1).fill(0));
155+
for (let i = 0; i <= n; i++) {
156+
dp[i][0] = 1;
157+
}
158+
159+
for (let i = 1; i <= n; i++) {
160+
for (let j = 1; j <= amount; j++) {
161+
if (coins[i - 1] > j) {
162+
dp[i][j] = dp[i - 1][j];
163+
} else {
164+
dp[i][j] = dp[i - 1][j] + dp[i][j - coins[i - 1]];
165+
}
166+
}
167+
}
168+
169+
return dp[n][amount];
170+
}
171+
172+
let coins = [1, 2, 5];
173+
let amount = 5;
174+
console.log("Number of ways to make change:", coinChange(coins, amount));
175+
176+
```
177+
178+
## Complexity Analysis
179+
180+
- Time Complexity: $O(n \cdot \text{amount})$, where n is the number of coins and amount is the given amount.
181+
- Space Complexity: $O(n \cdot \text{amount})$, for the DP table.
182+
183+
## Conclusion
184+
185+
Dynamic programming offers an efficient solution to the Coin Change Problem by breaking it down into subproblems and storing intermediate results. This technique helps determine the number of ways to make change for a given amount using a specific

0 commit comments

Comments
 (0)