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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:15:38+00:00 2026-05-26T14:15:38+00:00

I am currently working through a book on algorithm design and came across a

  • 0

I am currently working through a book on algorithm design and came across a question whereby you must implement a greedy algorithm with dynamic programming to solve the coin change problem.

I was trying to implement this and I just can’t figure out out or make sense of the algorithm given in my book. The algorithm is as follows(with my (lack of) understanding in comments) :

Change(p) {
  C[0] = 0
    for(i=1 to p) //cycling from 1 to the value of change we want, p
      min = infinity
        for(j=1 to k( //cyle from 1 to...?
          if dj <=i then
            if(1+C[i-dj] < min) then
               min = 1+C[i-dj]
            endif
          endif
        endfor
     C[i] = min
    endfor
  return C[p]
}

And my attempt at interpreting what’s going on :

/**
     * 
     * @param d
     *            currency divisions
     * @param p
     *            target
     * @return number of coins
     */
    public static int change(int[] d, int p) {
        int[] tempArray = new int[Integer.MAX_VALUE]; // tempArray to store set
                                                        // of coins forming
                                                        // answer
        for (int i = 1; i <= p; i++) { // cycling up to the wanted value
            int min = Integer.MAX_VALUE; //assigning current minimum number of coints
            for (int value : d) {//cycling through possible values
                if (value < i) {
                    if (1 + tempArray[i - value] < min) { //if current value is less than min
                        min = 1 + tempArray[1 - value];//assign it
                    }
                }
            }
            tempArray[i] = min; //assign min value to array of coins
        }
        System.out.println("help"); // :(
        return tempArray[p];
    }

Can someone please explain to me what I am missing, how to fix this, and how this algorithm is supposed to work? Dynamic Programming seems like such a useful tool, but I cannot get my head around it. I keep thinking recursively.

  • 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-05-26T14:15:39+00:00Added an answer on May 26, 2026 at 2:15 pm

    see this wikipedia link

    an excerpt:

    Greedy choice property We can make whatever choice seems best at the
    moment and then solve the subproblems that arise later. The choice
    made by a greedy algorithm may depend on choices made so far but not
    on future choices or all the solutions to the subproblem. It
    iteratively makes one greedy choice after another, reducing each given
    problem into a smaller one. In other words, a greedy algorithm never
    reconsiders its choices. This is the main difference from dynamic
    programming, which is exhaustive and is guaranteed to find the
    solution. After every stage, dynamic programming makes decisions based
    on all the decisions made in the previous stage, and may reconsider
    the previous stage’s algorithmic path to solution.

    Your code iterates through the int p gets the optimum choice and puts it into the array tempArray then uses this value to check the optimum choice in the next iteration.

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

Sidebar

Related Questions

I'm currently working my way through Accelerated C++ and just came across this in
I'm currently working through some exercises in a c++ book, which uses text based
I'm currently working through the excercises in 'The C Programming Language'. Here's one of
I am currently working through the famous Cocoa Programming for OSX by Aaron Hillegaas.
I am currently working through the Gregory Brown Ruby Best Practices book. Early on,
I'm currently working my way through this book: http://www1.idc.ac.il/tecs/ I'm currently on a section
I'm working through a jQuery book and am currently on the chapter of table
I am currently working through the Well Grounded Rubyist. Great book so far. I
I'm working through the exercises in the K&R book. Currently I'm stuck at exercise
I'm currently working my way through the book Teach Yourself Android Application Development in

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.