coin-change

Minimum coin change problem with limited amount of coins

我的梦境 提交于 2021-02-08 04:46:56
问题 To be specific, the problem is: Given array of denominations coins[] , array of limit for each coins limits[] and number amount , return minimum number of coins needed, to get the amount , or if it's not possible return null. Additionally fill array change with number of each coin used in the solution. This is my solution: public static int? Dynamic(int amount, int[] coins, int[] limits, out int[] change) { int[] minCoins = new int[amount + 1]; int[,] coinsUsedToAmount = new int[coins.Length,

Recursive algorithm to solve change-making problem

若如初见. 提交于 2021-01-28 09:34:56
问题 I want to make a recursive algorithm that solves the change-making problem. Is it possible to use a non-dynamic approach that not only returns the minimum number of coins but also returns the set of coins used to make-up the given value, For example, given the value 6 and the set of coins=[1, 3, 4]. Is it possible to make a recursive algorithm that doesn't memoise that can return both the minimum number of coins (2) and the set of coins (3,3)? EDIT: This is my current algorithm but it only

Coin change using dynamic programming

安稳与你 提交于 2021-01-21 09:50:52
问题 I've been working on coin change problem using dynamic programming. I've tried to make an array fin[] which contains the minimum number of coins required for that index and then print it. I've written a code which I think should give correct output but I can't figure out why it is not giving exact answer. For eg: for the input: 4 3 1 2 3 (4 is the amount to change, 3 the number of types of available coins, 1 2 3 is the list of coin values) The output should be: 0 1 1 1 2 (as we have 1,2,3 as

Coin Change Algorithm JS

≡放荡痞女 提交于 2020-12-15 04:56:13
问题 I have been trying to come up with a solution for this algorithm for 3-4 days but nothing seems to work and the available solutions are a bit more advanced for me. It has to be solved with conditionals only so no recursion or dynamic programming. I need to determine the least amount of coins necessary to give change given the following denominations: 1, 0.5, 0.2, 0.1, 0.05, 0.02 and 0.01. Input is the following: Price of an item Sum paid by customer Current ideas: let price = +gets(); let

Arithmetics in Prolog, represent a number using powers of 2

泄露秘密 提交于 2020-07-18 23:00:48
问题 I have two numbers, let's name them N and K , and I want to write N using K powers of 2. For example if N = 9 and K = 4 , then N could be N = 1 + 2 + 2 + 4 ( 2^0 + 2^1 + 2^1 + 2^2 ). My program should output something like N = [1,2,2,4] . I am used to C++. I can't find a way to solve this problem in Prolog. Any help will be appreciated! 回答1: Here's a scheme that uses CLP(FD). In general, when reasoning in the domain of integers in Prolog, CLP(FD) is a good way to go. The idea for this

Arithmetics in Prolog, represent a number using powers of 2

社会主义新天地 提交于 2020-07-18 23:00:20
问题 I have two numbers, let's name them N and K , and I want to write N using K powers of 2. For example if N = 9 and K = 4 , then N could be N = 1 + 2 + 2 + 4 ( 2^0 + 2^1 + 2^1 + 2^2 ). My program should output something like N = [1,2,2,4] . I am used to C++. I can't find a way to solve this problem in Prolog. Any help will be appreciated! 回答1: Here's a scheme that uses CLP(FD). In general, when reasoning in the domain of integers in Prolog, CLP(FD) is a good way to go. The idea for this

Representing an amount of money with specific bills

笑着哭i 提交于 2020-01-05 07:59:53
问题 I want to write a function in Racket which takes an amount of money and a list of specific bill-values, and then returns a list with the amount of bills used of every type to make the given amount in total. For example (calc 415 (list 100 10 5 2 1)) should return '(4 1 1 0 0) . I tried it this way but this doesn't work :/ I think I haven't fully understood what you can / can't do with set! in Racket, to be honest. (define (calc n xs) (cond ((null? xs) (list)) ((not (pair? xs)) (define y n)

Python Coin Change Dynamic Programming

寵の児 提交于 2020-01-03 06:31:09
问题 I am currently trying to implement dynamic programming in Python, but I don't know how to setup the backtracking portion so that it does not repeat permutations. For example, an input would be (6, [1,5]) and the expected output should be 2 because there are 2 possible ways to arrange 1 and 5 so that their sum is equivalent to 6. Those combinations are {1,1,1,1,1,1} and {1,5} but the way my program currently works, it accounts for the combinations displayed above and the combination {5,1}.