Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create CoinChange.java #390

Closed
wants to merge 1 commit into from
Closed

Conversation

anill1412
Copy link

The Coin Change Problem is a classic dynamic programming (DP) problem where the goal is to find the minimum number of coins required to make a given amount. Given an unlimited supply of certain denominations, you must find the minimum number of coins that sum up to a specified amount. If it’s impossible to make that amount with the given coins, the function should return -1.

Problem Statement:
You are given an integer array coins representing different denominations of coins, and an integer amount representing a total amount of money. The task is to return the minimum number of coins needed to make up that amount.

Example:
Input:

coins = [1, 2, 5]
amount = 11
Output:

3 (11 = 5 + 5 + 1)
Explanation:

To make the amount 11 with the fewest coins, you use two 5-coin denominations and one 1-coin denomination. Approach:
This problem can be solved efficiently using Dynamic Programming. The main idea is to build a DP table where each entry at index i represents the minimum number of coins needed to make the amount i.

Define the State:

Let dp[i] represent the minimum number of coins required to make the amount i. Recurrence Relation:

For each amount i, iterate through each coin c in coins. If i - c >= 0 (meaning the coin can contribute to this amount), update dp[i] as follows: 𝑑
𝑝
[
𝑖
]

min

(
𝑑
𝑝
[
𝑖
]
,
𝑑
𝑝
[
𝑖

𝑐
]
+
1
)
dp[i]=min(dp[i],dp[i−c]+1)
Here, dp[i - c] + 1 represents using one coin of denomination c and the minimum coins needed to make the remaining amount i - c. Base Case:

dp[0] = 0 because no coins are needed to make an amount of 0. Result:

If dp[amount] is still infinity (or a high number), it means it’s impossible to make the amount with the given coins, so return -1. Otherwise, return dp[amount].

The Coin Change Problem is a classic dynamic programming (DP) problem where the goal is to find the minimum number of coins required to make a given amount. Given an unlimited supply of certain denominations, you must find the minimum number of coins that sum up to a specified amount. If it’s impossible to make that amount with the given coins, the function should return -1.

Problem Statement:
You are given an integer array coins representing different denominations of coins, and an integer amount representing a total amount of money. The task is to return the minimum number of coins needed to make up that amount.

Example:
Input:

coins = [1, 2, 5]
amount = 11
Output:

3 (11 = 5 + 5 + 1)
Explanation:

To make the amount 11 with the fewest coins, you use two 5-coin denominations and one 1-coin denomination.
Approach:
This problem can be solved efficiently using Dynamic Programming. The main idea is to build a DP table where each entry at index i represents the minimum number of coins needed to make the amount i.

Define the State:

Let dp[i] represent the minimum number of coins required to make the amount i.
Recurrence Relation:

For each amount i, iterate through each coin c in coins. If i - c >= 0 (meaning the coin can contribute to this amount), update dp[i] as follows:
𝑑
𝑝
[
𝑖
]
=
min
⁡
(
𝑑
𝑝
[
𝑖
]
,
𝑑
𝑝
[
𝑖
−
𝑐
]
+
1
)
dp[i]=min(dp[i],dp[i−c]+1)
Here, dp[i - c] + 1 represents using one coin of denomination c and the minimum coins needed to make the remaining amount i - c.
Base Case:

dp[0] = 0 because no coins are needed to make an amount of 0.
Result:

If dp[amount] is still infinity (or a high number), it means it’s impossible to make the amount with the given coins, so return -1. Otherwise, return dp[amount].
Copy link
Owner

@panditakshay402 panditakshay402 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kindly follow all the rules from readme.md file

@anill1412 anill1412 closed this by deleting the head repository Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants