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 9255755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:46:53+00:00 2026-06-18T11:46:53+00:00

I’m trying to understand the coin change problem solution, but am having some difficulty.

  • 0

I’m trying to understand the coin change problem solution, but am having some difficulty.

At the Algorithmist, there is a pseudocode solution for the dynamic programming solution, shown below:

n = goal number
S = [S1, S2, S3 ... Sm]

function sequence(n, m)
   //initialize base cases

   for i = 0 to n
     for j = 0 to m
       table[i][j] = table[i-S[j]][j] + table[i][j-1]

This is a pretty standard O(n^2) algorithm that avoids recalculating the same answer multiple times by using a 2-D array.

My issue is two-fold:

  1. How to define the base cases and incorporate them in table[][] as initial values
  2. How to extract out the different sequences from the table

Regarding issue 1, there are three base cases with this algorithm:

  • if n==0, return 1
  • if n < 0, return 0
  • if n >= 1 && m <= 0, return 0

How to incorporate them into table[][], I am not sure. Finally, I have no idea how to extract out the solution set from the array.

  • 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-18T11:46:54+00:00Added an answer on June 18, 2026 at 11:46 am

    We can implement a dynamic programming algorithm in at least two different approaches. One is the top-down approach using memoization, the other is the bottom-up iterative approach.

    For a beginner to dynamic programming, I would always recommend using the top-down approach first since this will help them understand the recurrence relationships in dynamic programming.

    So in order to solve the coin changing problem, you’ve already understood what the recurrence relationship says:

    table[i][j] = table[i-S[j]][j] + table[i][j-1]
    

    Such a recurrence relationship is good but is not that well-defined since it doesn’t have any boundary conditions. Therefore, we need to define boundary conditions in order to ensure the recurrence relationship could successfully terminate without going into an infinite loop.

    So what will happen when we try to go down the recursive tree?

    If we need to calculate table[i][j], which means the number of approaches to change i using coins from type 0 to j, there are several corner cases we need to handle:

    1) What if j == 0?

    If j == 0 we will try to solve the sub-problem table(i,j-1), which is not a valid sub-problem. Therefore, one boundary condition is:

    if(j==0) {
      if(i==0) table[i][j] = 1;
      else table[i][j] = 0;
    }
    

    2) What if i - S[j] < 0?

    We also need to handle this boundary case and we know in such a condition we should either not try to solve this sub-problem or initialize table(i-S[j],j) = 0 for all of these cases.

    So in all, if we are going to implement this dynamic programming from a top-down memoization approach, we can do something like this:

    int f(int i, int j) {
      if(calc[i][j]) return table[i][j];
      calc[i][j] = true;
      if(j==0) {
        if(i==0) return table[i][j]=1;
        else return table[i][j]=0;
      }
      if(i>=S[j])
        return table[i][j]=table[i-S[j][j]+table[i][j-1];
      else
        return table[i][j]=table[i][j-1];
    }
    

    In practice, it’s also possible that we use the value of table arrays to help track whether this sub-problem has been calculated before (e.g. we can initialize a value of -1 means this sub-problem hasn’t been calculated).

    Hope the answer is clear. 🙂

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into

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.