Question

In: Computer Science

IMPLEMENT IN JAVA PLEASE I NEED THIS DONE ASAP Question 1 (25pt) Dynamic Programming – Coin...

IMPLEMENT IN JAVA PLEASE I NEED THIS DONE ASAP

Question 1 (25pt) Dynamic Programming – Coin Change

Design algorithm for the coin change problem using dynamic programming approach. Given coin denominations by value: c1 < c2 < … < cn, and change amount of x, find out how to pay amount x to customer using fewest number of coins.

Your algorithm needs to work for ANY coin denomination c1 < c2 < … < cn, and ANY change amount of x. For example, given coin denominations 1, 5, 10, 25, x=78, the solution is {3 coins of 25 and 3 coins of 1, total 6 coins}.

NEED TO DO ALL 3 PARTS

• Present your algorithm in pseudo code.

• Trace your algorithm and show how it works on a small example, show the step-by-step process and the final result.

• Analyze the time complexity of the algorithm and present the result using order notation.

Solutions

Expert Solution

As per your question, The pseudo code and java 8 program is written correctly and perfectly. Please have a look on the source code(please refer in-line comments for better understanding) and the run output below:-

COIN-CHANGE-MODIFIED(coins, coinsCount, toBeChangeValue)
  M[coinsCount+1]
  M[0] = 0

  S[coinsCount+1]
  S[0] = 0

  for j in 1 to coinsCount
    minimum = INF

    for i in 1 to toBeChangeValue
      if j >= coins[i]
        if 1+M[j-coins[i]] < minimum
          minimum = 1+M[j-coins[i]]
          coin = i

    M[j] = minimum
    S[j] = coin

  //Code to print the coins, given below
  ...
  return M[coinsCount]

=> The Java 8 program as per your question(please refer in-line comments for better understanding):-

import java.util.Scanner;

public class coin{

        //creating the scanner object
        private static Scanner scan = new Scanner(System.in);

        //the main driver method
        public static void main(String[] args){
                System.out.println("Enter the coins value(space separated): ");
                //taking space separated coins value from user
                String s[]= scan.nextLine().split(" ");

                //creating the coins integer array for holding coins value
                int coins[] = new int[s.length];

                //copying the coins value into the coins integer array from string array
                for(int i =0 ;i < s.length;i++){
                        coins[i]= Integer.parseInt(s[i]);
                }
                
                System.out.print("Enter the Value: ");
                //taking the x value that require to change 
                int xValue = scan.nextInt();

                System.out.println("At least " + coinChangeProblem(coins, xValue)
                + " coins are required to make a value of " + xValue + " from given coins");
        }
        /*
         * min method, compare and return min value
         * @param a - int
         * @param b - int
         * @returns min - int
         */
        public static int min(int a, int b){
                int min = (a <= b) ? a : b;
                return min;
        }
        /*
         * @param coins array
         * @param xValue - value for which need changes
         * @return the minimum no of coins required to change the xValue(sum)
         */
        public static int coinChangeProblem(int[] coins, int sum){ 

                //getting number of coins -> coins count
                int totalCoins = coins.length;

                // Creating array which stores subproblems' solutions
                int[][] arr = new int[totalCoins + 1][sum + 1];

                // Initialising first row with +ve infinity
                for(int j = 0; j <= sum; j++){
                        arr[0][j] = Integer.MAX_VALUE;
                }             

                // Initialising first column with 0
                for(int i = 1; i <= totalCoins; i++){
                        arr[i][0] = 0;
                }

                // Implementing the recursive solution
                for(int i = 1; i <= totalCoins; i++){
                        for(int j = 1; j <= sum; j++){
                                if(coins[i - 1] <= j)
                                        arr[i][j] = min(1 + arr[i][j - coins[i - 1]], arr[i - 1][j]);
                                else
                                        arr[i][j] = arr[i - 1][j];
                        }
                }
                //returning the needed minimum no of coins 
                return (int)arr[totalCoins][sum];
        }
}

=> The Run output(as per your example):-

I hope, now you have crestal and clear undersatnding of Coin Change Problem and Its Dynamic Programming Solution.

Please don't forget to like it...

Thank You...


Related Solutions

Please Why do we need a dynamic stack? How to implement a dynamic array stack?(JAVA)
Please Why do we need a dynamic stack? How to implement a dynamic array stack?(JAVA)
Please I need this to be done in Java, can I have it answered by anonymous...
Please I need this to be done in Java, can I have it answered by anonymous who answered my last question regarding java? Thank you You will need to implement a specific algorithm implementation to match the class definition AND implement a unit test using JUnit that conforms the specific naming convention. Algorithm: Inputs: integers m and n , assumes m and n are >= 1 Order is not important for this algorithm Local variables integers: remainder Initialize: No initialization...
C programming - Implement a dynamic array, please read and implement the dynarray.c file with what...
C programming - Implement a dynamic array, please read and implement the dynarray.c file with what is given dynarray.h /* * This file contains the definition of the interface for the dynamic array * you'll implement. You can find descriptions of the dynamic array functions, * including their parameters and their return values, in dynarray.c. You * should not modify anything in this file. */ #ifndef __DYNARRAY_H #define __DYNARRAY_H /* * Structure used to represent a dynamic array. */ struct...
IN JAVA PLEASE ASAP !!! I just need the main and mergesort function Ask the user...
IN JAVA PLEASE ASAP !!! I just need the main and mergesort function Ask the user for the number of elements, not to exceed arraySize = 20 (put appropriate input validation) Ask the user for the type of data they will enter - EnglishGrade or MathGrade objects. Use your EnglishGrade and MathGrade classes Based on the input, create an appropriate array for the data to be entered. Write a helper function called recursionMergeSort such that: It is a standalone function...
Implement a queue - C programming, please read the instruction carefully and implement queue.c using dynamic...
Implement a queue - C programming, please read the instruction carefully and implement queue.c using dynamic array structure given dynarray.h and dynarray.c below The second ADT you'll implement for this assignment is a queue. For this assignment, the interface for the queue (i.e. the structures and the prototypes of functions a user of the queue interacts with) is already defined for you in the file queue.h. Your first task in this assignment is to implement definitions for the functions that...
I need this in C# please. Exercise #2: Design and implement a programming (name it NextMeeting)...
I need this in C# please. Exercise #2: Design and implement a programming (name it NextMeeting) to determine the day of your next meeting from today. The program reads from the user an integer value representing today’s day (assume 0 for Sunday, 1 for Monday, 2 for Tuesday, 3 for Wednesday, etc…) and another integer value representing the number of days to the meeting day. The program determines and prints out the meeting day. Format the outputs following the sample...
Please answer this question ASAP an explain in detail. I really need it before the night...
Please answer this question ASAP an explain in detail. I really need it before the night is over. a) Explain what a catalytic process is and how it is different from the non-catalytic version of the process. Give an example of an enzymatic process other than amylase and describe the substrate(s)/product(s): b) Describe what the enzyme amylase does and what would happen in the absence of this enzyme:
(JAVA) Why do we need a dynamic stack? How do you implement a dynamic stack array?
(JAVA) Why do we need a dynamic stack? How do you implement a dynamic stack array?
i need a conclusion for my paper on ADHD. I need this asap please have to...
i need a conclusion for my paper on ADHD. I need this asap please have to turn my paper in one hour!!!! thank you            In the recent times, ADHD is being seen in the light of cerebral dysfunction with too much focus on clinical treatment using medication at the same time keeping psychotherapy at bay which shows a dangerous trend. Psychotherapy even though cannot explain the cause of the condition, but it can help the children in coping his...
can you please do this question? i need the result asap. show the output as well...
can you please do this question? i need the result asap. show the output as well Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT