Minimum Coin Count For a Coin Change Sum Problem
💻 coding

Minimum Coin Count For a Coin Change Sum Problem

1 min read 125 words
1 min read
ShareWhatsAppPost on X
  • 1The coin change problem aims to determine the minimum number of coins needed to achieve a specific total using given denominations.
  • 2Dynamic programming is utilized to solve the problem efficiently by building a table that tracks the minimum coins for each sub-total.
  • 3The solution's time complexity is O(n*m), where n is the number of coin types and m is the target sum.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The coin change problem aims to determine the minimum number of coins needed to achieve a specific total using given denominations."

Minimum Coin Count For a Coin Change Sum Problem

Coin Changing Minimum Count Number of Coins With a Sum Using Dynamic programming.

The question is given coins of certain denominations and a total, how many minimum coins would you need to make this total.

public class MinCoinChangeProblem {
	private static int min(int a, int b) {
		return a<b?a:b;
	}
	
	public static void main(String[] args) {
		int[] coins = new int[] {0, 1, 5, 6, 8};
		int total = 11;
		int[][] dp = new int[coins.length][total+1];
		
		for(int i=0;i<coins.length;i++) {
			for(int j=0;j<total+1;j++) {
				
				if(j==0)
					dp[i][j]=0;
				else if(i==0)
					dp[i][j]=Integer.MAX_VALUE;
				else if(j<coins[i]) {
					dp[i][j]=dp[i-1][j];
				}
				else {
					dp[i][j]=min(dp[i-1][j],dp[i][j-coins[i]]+1);
				}
			}
		}
		
		System.out.println(dp[coins.length-1][total]);
	}
}

The time complexity of the solution is n*m. where n is the total number of different coins available and m is the total sum required.

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 13 July 2018 · 1 min read · 125 words

Part of AskGif Blog · coding

You might also like