Coin Change in Python and Java
In this tutorial, we will learn LeetCode #322: Coin Change in very simple language. We will understand the idea step by step, see custom examples, and write complete code in Python and Java.
What Is the Coin Change Problem?
This problem asks us to solve a common coding interview task using the given input. The goal is to return the correct result without using a slow brute force method.
For this problem, we will use dynamic programming because it gives a clean and optimized solution.
Example input: coins = [1, 4, 6], amount = 8 Expected result: Output: 2 Explanation: Use 4 + 4.
Beginner-Friendly Idea
The main idea is to avoid trying every possible answer blindly. Instead, we keep useful information while reading the input and use that information to make the next decision.
At each step, ask: “What do I already know, and how does the current value change my answer?”
Using our example: coins = [1, 4, 6], amount = 8 Approach: dynamic programming We update variables step by step until we reach: Output: 2
Why Do We Use This Approach?
A direct brute force solution is usually easier to think about, but it can become slow when the input is large. The optimized approach keeps only the important state and avoids repeated work.
That is why dynamic programming is useful for this problem.
Step-by-Step Explanation
Let us dry run the algorithm using a custom example.
Step 1 Use this input. coins = [1, 4, 6] amount = 8 Create dp where dp[x] means minimum coins needed for amount x. dp[0] = 0 Step 2 For amount 4, coin 4 can be used. dp[4] = dp[4 - 4] + 1 dp[4] = dp[0] + 1 dp[4] = 1 Step 3 For amount 6, coin 6 can be used. dp[6] = dp[0] + 1 dp[6] = 1 Step 4 For amount 8, coin 4 can be used twice. dp[8] = dp[8 - 4] + 1 dp[8] = dp[4] + 1 dp[8] = 1 + 1 dp[8] = 2 Final answer = 2
Important Code Logic
The most important part is updating the algorithm state after reading each useful value. This is where the answer becomes better step by step.
Think like this: old_state = what we knew before current_value = value we are checking now new_state = updated result after using current_value For our example, the final state gives: Output: 2
Example 1
Input: coins = [1, 4, 6], amount = 8 Output: Output: 2 Explanation: Use 4 + 4.
Example 2
Input: coins = [1, 3, 5], amount = 11 Output: 3 Explanation: Use 5 + 5 + 1 to make 11 with 3 coins.
Python Code
Here is the complete Python solution for LeetCode #322.
class CoinChangeCalculator:
def coin_change(self, coins, amount):
dp = [amount + 1] * (amount + 1)
dp[0] = 0
for value in range(1, amount + 1):
for coin in coins:
if value >= coin:
dp[value] = min(dp[value], dp[value - coin] + 1)
return dp[amount] if dp[amount] != amount + 1 else -1
calculator = CoinChangeCalculator()
print(calculator.coin_change([1, 4, 6], 8)) # Output: 2
print(calculator.coin_change([2], 3)) # Output: -1Java Code
Here is the complete Java solution for LeetCode #322.
class CoinChangeCalculator {
public int coinChange(int[] coins, int amount) {
int[] dp = new int[amount + 1];
java.util.Arrays.fill(dp, amount + 1);
dp[0] = 0;
for (int value = 1; value <= amount; value++) {
for (int coin : coins) {
if (value >= coin) {
dp[value] = Math.min(dp[value], dp[value - coin] + 1);
}
}
}
return dp[amount] == amount + 1 ? -1 : dp[amount];
}
public static void main(String[] args) {
CoinChangeCalculator calculator = new CoinChangeCalculator();
System.out.println(calculator.coinChange(new int[] {1, 4, 6}, 8)); // Output: 2
System.out.println(calculator.coinChange(new int[] {2}, 3)); // Output: -1
}
}Time and Space Complexity
Time Complexity: O(n)
The time complexity depends on how many values the algorithm needs to process and whether it uses sorting, binary search, heap, or traversal.
Space Complexity: O(1)
The extra space is used for the variables or data structures needed by the optimized approach.
Final Summary
LeetCode #322: Coin Change becomes easier when we break it into small steps. First understand what the problem asks, then track the important state, dry run with an example, and finally write the code.