Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8562521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:44:52+00:00 2026-06-11T16:44:52+00:00

Given a target amount and a list of coin denominations, my code is supposed

  • 0

Given a target amount and a list of coin denominations, my code is supposed to find the fewest coins needed to reach the target amount.

Examples:

  • C(78, [1, 5, 10, 25, 50]) = 6

    • we can make 78 from 3x25 + 3x1, so 6 coins are required
  • C(48, [1, 7, 24, 42]) = 2

    • 48 = 2x24, so 2 coins are sufficient
  • C(35, [1, 3, 16, 30, 50]) = 3

    • we can make 35 from 2x16 + 1x3, so 3 coins suffice

I made the code with for loops, but how do I make it recursive?

def C(i, coins, cdict = None):
    if cdict == None:
        cdict = {}
    if i <= 0:
        cdict[i] = 0
        return cdict[i]
    elif i in cdict:
        return cdict[i]
    elif i in coins:
        cdict[i] = 1
        return cdict[i]
    else:
        min = 0
        for cj in coins:
            result = C(i - cj, coins)
            if result != 0:
                if min == 0 or (result + 1) < min:
                    min = 1 + result
        cdict[i] = min
        return cdict[i]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:44:53+00:00Added an answer on June 11, 2026 at 4:44 pm

    It’s the change-making problem. Here’s the standard recursive solution, V is the list of coins and C the target amount of money:

    def min_change(V, C):
        def min_coins(i, aC):
            if aC == 0:
                return 0
            elif i == -1 or aC < 0:
                return float('inf')
            else:
                return min(min_coins(i-1, aC), 1 + min_coins(i, aC-V[i]))
        return min_coins(len(V)-1, C)
    

    And this is an optimized version, using dynamic programming:

    def min_change(V, C):
        m, n = len(V)+1, C+1
        table = [[0] * n for x in xrange(m)]
        for j in xrange(1, n):
            table[0][j] = float('inf')
        for i in xrange(1, m):
            for j in xrange(1, n):
                aC = table[i][j - V[i-1]] if j - V[i-1] >= 0 else float('inf')
                table[i][j] = min(table[i-1][j], 1 + aC)
        return table[m-1][n-1]
    

    Both implementations will always return the optimal solution, but the second one will be much faster for large inputs. Notice that the greedy algorithm suggested in other answers gives an optimal solution only for certain combinations of currency – for instance, it works for the American coins.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I search and find, for a given target value, the closest value
how can i get immediate parent of a given element ? $(e.target).parent() ?
Given this code: class Overloading extends Object { static public void target(Object val, String
I have modified my MOSS 2007 configuration to query a given target AD successfully.
Given the following code: $(.force-selection).blur(function() { var value = $('matched-item').val(); //check if the input's
Problem Step 1 : Given a list of numbers, generate all possible groupings (in
Is there a specific version of .Net that I can target that is bundled
Is there any way to say that if prerequisite for the given target doesn't
Given a large unordered array of long random numbers and a target long, what's
How to check if target of a given path is a directory? Say: rem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.