Min Coin | Practice | GeeksforGeeks

Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
 
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2 
denominations of 5 and 1 denomination of  
1, one can make 11.

Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using 
denominations 2 and 6.

 
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
 
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
 
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 104
0
vibhutisingh2 weeks ago
def MinCoin(self, nums, amount):
		# Code here
		dp=[(amount+1)]*(100001)
		dp[0]=0
		for i in range(len(nums)):
		    for j in range(1,amount+1):
		        if nums[i]<=j:
		            dp[j]=min(dp[j],dp[j-nums[i]]+1)
		if(dp[amount]>amount):
		    return -1
		return dp[amount]
0
vibhutisingh
This comment was deleted.
+1
spider72 weeks ago
int MinCoin(vector<int>nums, int amount){   vector<int> dp(amount+1,amount+1);   dp[0] = 0;   for(int i=0;i<=amount;i++){       for(int j=0;j<nums.size();j++){           if(nums[j]<=i){               dp[i] = min(dp[i],1+dp[i-nums[j]]);           }       }   }   return dp[amount]==amount+1? -1 : dp[amount];}
0
imranwahid2 weeks ago
Easy C++ solution using memoization
0
shaileshbaba7282 weeks ago
int MinCoin(vector<int>nums, int amount)
	{
	   //can take any value greater than amount like:amount+1,amount+2,10001 or INT_MAX	
	   vector<int> dp(amount+1,10001);
	   //now enter the value for dp[0]=0
	   dp[0]=0;
	   // it is based on three steps,
	   // 1. pick the current coin=>dp[j-nums[i]]+1, here +1 for picking this coin
	   // 2. dont pick the current coin=>dp[i] value remains same
	   // 3. minimum of both of them  
	   for(int i=0;i<nums.size();i++)
	   {
	       for(int j=1;j<=amount;j++)
	       {
	           if(nums[i]<=j)
	            dp[j]=min(dp[j],dp[j-nums[i]]+1);
	       }
	   }
	   if(dp[amount]>amount)
	    return -1;
	   return dp[amount]; 
	}
0
rohitjug19csPremium2 weeks ago
JAVA Solution Linear DP
public int MinCoin(int[] nums, int amount)
    {
       int dp[] = new int[amount+1];
       Arrays.fill(dp, Integer.MAX_VALUE);
       dp[0] = 0;
       for(int amt=0; amt<=amount; amt++){
          int min = Integer.MAX_VALUE;
          for(int coin : nums)
              if(coin <= amt)min = Integer.min(min, dp[amt-coin]);
          
          if(min != Integer.MAX_VALUE)dp[amt] = 1 + min;
       }
       
       return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
    }
0
prasantpoudel332 weeks ago
def MinCoin(self, nums, amount):
		# Code here
		n = len(nums) 
        i = n - 1
        ans=0
        while(i >= 0):
            while (amount >= nums[i]):
                amount -= nums[i]
                ans+=1
            i -= 1
        if ans!=0:
            return ans
        return -1
i am getting error in one of the test cases?
2 2512 6
this kind of input.
what kind of format is this
0
abhishekkaswan2 weeks ago
Easy C++ solution
int MinCoin(vector<int>nums, int amount)
	{
	   //can take any value greater than amount like:amount+1,amount+2,10001 or INT_MAX	
	   vector<int> dp(amount+1,10001);
	   //now enter the value for dp[0]=0
	   dp[0]=0;
	   // it is based on three steps,
	   // 1. pick the current coin=>dp[j-nums[i]]+1, here +1 for picking this coin
	   // 2. dont pick the current coin=>dp[i] value remains same
	   // 3. minimum of both of them  
	   for(int i=0;i<nums.size();i++)
	   {
	       for(int j=1;j<=amount;j++)
	       {
	           if(nums[i]<=j)
	            dp[j]=min(dp[j],dp[j-nums[i]]+1);
	       }
	   }
	   if(dp[amount]>amount)
	    return -1;
	   return dp[amount]; 
	}
0
akshitasinghal44442 weeks ago
C++ | DP
Time - O(n*amt)
Space - O(amt)
		int n=a.size();
	    vector<int> dp(amt+1,INT_MAX);
	    dp[0]=0;
	    int i,j;
	    
	    for(i=0;i<n;i++)
	    {
	        for(j=1;j<=amt;j++)
	        {
	            if(a[i]<=j && dp[j-a[i]]!=INT_MAX)
	            dp[j]=min(dp[j-a[i]]+1,dp[j]);
	        }
	    }
	    
	    return dp[amt]==INT_MAX?-1:dp[amt];
0
alistair01112 weeks ago
int MinCoin(vector<int>nums, int amount)
{
    // Code here
    vector<int>dp(amount+1, INT_MAX);
    dp[0] = 0;
    
    for(int i=1; i<amount+1; i++){
        int temp = INT_MAX;
        for(int j = 0; j<nums.size(); j++){
            if(i-nums[j]>=0){
                temp = min(temp, dp[i-nums[j]]);
            }
        }
        if(temp==INT_MAX){
            dp[i] = temp;
        }else{
            dp[i] = temp+1;
        }
       // cout<<dp[i]<<" "<<i<<endl;
    }
    return dp[amount]==INT_MAX?-1:dp[amount];
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.