## The Coin Change Problem

- JK
jppkid + 39 comments watch out for integer overflow :). i had to switch to 64-bit int.

AlphaBeta55 + 1 comment Thank you

- T
tashia + 2 comments Thank you !

hargar + 0 comments thanks

- MK
kumar_malay888 + 1 comment this is my code and used java8 :- for coin change problem

public static String CoinChange(int input1,int[] input2) { return CoinChange(input1,input2,0,new HashMap()); }

`public static String CoinChange(int input1,int[] input2,int a, HashMap<String,Long> memo) { int b = 1; int c = 0; long d = 0; String x = ""; if(input1 == 0) { return (x = String.valueOf(b)); } if(a >= input2.length) { return (x = String.valueOf(c)); } String z = input1+"-"+a; if(memo.containsKey(z)) { d = memo.get(z); return (x = String.valueOf(d)); } int e = 0; long f = 0; while(e <= input1) { int g = input1 - e; f += CoinChange(input2, g, a+1,memo); e += input2[a]; } memo.put(z,f); return(x = String.valueOf(f)); and i was getting a error which is CandidateCode.java:43: error: incompatible types: int[] cannot be converted to int f += CoinChange(input2, g, a+1,memo); can anyone please solve this`

- IB
iybaron25 + 0 comments The first argument for your method "CoinChange" takes an int. You are passing it "input2" which is an int array.

acodebreaker + 2 comments thanks

Dream_machine + 1 comment thanks :)

hargar + 1 comment thanks

- PT
phuadc123 + 1 comment thanks

mohitduklan + 0 comments thanks

amazinghacker + 4 comments Do we have to use recursion with dynamic programming to solve this?

- DR
introvert + 0 comments [deleted] robbyoconnor + 1 comment dynamic programming is really just caching previous work

oubenal + 0 comments Memoization*

- P
piyusman + 3 comments public static long count(long sum,long[] coins , int coin_num){

`if(sum==0){ return 1; } else if(sum<0){ return 0; } else if(coin_num<=0){ return 0; } else if(memo[(int)sum][coin_num]!=-1){ return memo[(int)sum][coin_num]; } else{ memo[(int)sum][coin_num]=count(sum,coins,coin_num-1)+count(sum-coins[coin_num-1],coins,coin_num); return memo[(int)sum][coin_num]; } }`

Here we are caching the previous result in memo multidimensional array. First you need to initialize it wih some number(you can use Arrays.fill for that) Also I tried to make it clear what memo is really doing. Means if the ways for particular sum is already there then no need to calulate again. Else go ahead and solve.

sum = amount

coins = array of all available coins.

coin_num is length of coins array(or you can say that next int in first line of this question's input)

So basically if you have

sum = 100;

coins[] = {25,10,5,1};

coin_num = 4;

memo[][] = new long[sum+1][coin_num+1];

and then call countWays(sum,coins,coin_num);

I have a question to others though, in java when I am trying to make it long I have to do a lot of casting, any other way to deal with the 'long' problem.

sushant001 + 1 comment Your comment helped, so here goes mine ... Rather than using 2D array, create another class (Let call it Change having long sum, int coin . override equals and hashcode- hashcode is really important , use hashcode = sum, that will be good enough ).

Now , in place of 2D array, use map (HashMap). and you are done.

Thanks

- A
andadeacu + 0 comments What can store in HashMap? For example: fot the first example: HashMap would contain (1,4), (1,2), (2,1) (2,2) (1,3)

bnegrao + 4 comments I don't understand why if sum == 0 then return 1; If sum is 0 and the coin values are >= 1, how can I have one solution if the sum is 0? if sum is 0 I should have 0 solutions...

- ZS
qwerjoe + 0 comments if your sum == 0 then you have already found a 'valid' variation of coins in the previous level of the recursion.

And if N = 0, the output should be 1 (zero from every coin type is the 1 and only good solution here)

mk9440 + 0 comments it is just as factorial of zero = 1 ie no of ways.

- AD
abhidash + 0 comments Because one valid solution is to not use any coin at all.

dewscoder + 0 comments when sum is zero, a valid selection of coins would be null set i.e., {}, and thats why return 1

- 정(
jiwan_chung + 1 comment I didn't understand

count(sum,coins,coin_num-1)+count(sum-coins[coin_num-1],coins,coin_num);

part, can anyone explain me kindly? I really want to understand DP... :(

- SK
sumeet_ram + 3 comments Its like, what you do for, finding the number of subsets of a set. Whether an element belongs to your set or it doesn't.

Here, either you use the last coin (in your set of given coins) to make change or you dont.

In case you dont use the last (say 'm'th) coin, now you are left with the subproblem of solving the toatal amount('sum' variable here) using onle the remaining 'm-1' coins ([0:m-2] in the array). We cant use the 'm'th coin because by our own assumption, its not part of the subproblem.

In case you use the last coin, you have the subproblem of solving "sum-Value('m'th coin)" using all the 'm' coins ( because using the 'm' th coin once doesnt bar you from using it again).

Finally you can add the count from both the cases as either of them are a possibility in your final solution.

Lemme know if this helped!!

Snigdha_Sanat + 1 comment @sumeet_ram I did not understand it still, can u please elaborate it a bit more? The choice of the 2 subproblems(count(sum,coins,coin_num-1) and count(sum-coins[coin_num-1],coins,coin_num)) do not look intuitive to me- what am I missing?

midnjerry + 2 comments Imagine it like a tree....

let's say we have n = 25 and c = [1,2,3]

we can break it down from 25 into 3 branches by:

25 = 1 + f(24)

25 = 2 + f(23)

25 = 3 + f(22)

each of these scenarios also branch off:

f(24) = 1 + f(23)

f(24) = 2 + f(22)

f(24) = 3 + f(21)

f(23) = 1 + f(22)

f(23) = 2 + f(21)

f(23) = 3 + f(20)

f(22) = 1 + f(21)

f(22) = 2 + f(20)

f(22) = 3 + f(19)

As you keep on propagating... all of the permutations that end up negative are invalid combinations. all of the permutations that end up exactly at 0 are good combinations.

this is a simplistic approach but it's not correct. Because it doesn't differentiate between... [2,2,5] and [5,2,2]. To get only unique combinations you need to remove 1 element from the C set everytime you iterate.

so... it'll look like this (in pseudo code).

static long getWays(long n, long[] c) { if (n < 0) { return 0; } if (n == 0) { return 1; } if (CacheValuePopulated(n)){ return CacheValue(n); } long sum = 0; for (int i = 0; i < c.length; i++) { long target = n - c[i]; long[] subc = subArrayOfCFrom(i to end); long result = getWays(target, subc); if (result > 0) { saveResultToYourCache(result); } sum += result; } return sum; }

`Finally... what made the cache problematic was that you can't just save values for any given n. You have to save values for n BASED on the C subArray you used to calculate it.`

Since the problem states that all values of c are unique, I used the first element of the c subarray as a key which pointed to its own n+1 array.

So for example, the cache could differentiate between running (5, [1,2,3]) and (5, [2,3])

f(5,[1,2,3]) = 4 (1,1,1,1,1), (1,1,1,2), (1,1,3), (2, 3) f(5,[2,3]) = 1 (2,3)

I hope this helps everyone. I didn't want to give the full answer away. Good luck!

ErikWitkowski + 1 comment but if you remove the element you just use you cannot have repeated elements like [2,2,5], can you?

- ZY
fzy1995 + 0 comments he didn't remove the element he just used. read again. he's referring to start from i to end, which means it includes the index of the element he just used.

malviyan_shiv + 0 comments Couldn't got how to solve saving to cache problem ?

sasuke29 + 0 comments Thank you so much for your explanation :)

- SK
santhos_kumar + 0 comments Greatly helped. Thankyou!

- NE
InfinityRedux + 0 comments There is a way of solving this without using recursion at all, just using dynamic programming. Instead of starting with "we want the solution for n" you can instead start with "what can we do with these coins".

ABcdexter19 + 4 comments Did it in python 8-)

Anachor + 8 comments For a moment I was thinking- "What is python 8?"

- MS
crazzy7 + 1 comment Me too

- AG
agodfrey3 + 1 comment same

- SA
agarwal_sumedha1 + 0 comments it is not passing the test case even if the expected value is same as the output

- GS
gsnanda + 0 comments [deleted] - RB
Coder_BPPIMT + 0 comments Same here

anhthong_381996 + 1 comment it's "8-)" which is a smile face and isn't "python 8".

hargar + 0 comments th@nks {@ is a not at}

- AS
srivastava_ambi1 + 0 comments python and then [8-)] -> smiley

- AY
Akhil_yadav + 0 comments it's python3

harpz_ + 0 comments lol.. me too :|

amazinghacker + 0 comments [deleted]anhthong_381996 + 0 comments [deleted]- FD
frandelarraniaga + 0 comments Can you upload the code?? I get it, but some tests get out of time.

My code:

def reverse(lista): list2 = [] for i in range(1,len(lista) + 1): list2.append(lista[len(lista) - i]) return(list2)

def listasinmax(lista, maxi): if (len(lista) == 0): return([])

`if (lista[0] <= maxi): return(lista) if (lista[0] > maxi): return(listasinmax(lista[1:], maxi))`

def getchange(value, lista): # Cantidad de cambios global cant_cambios

`if (len(lista) == 0): return(0) # Algorithm maxi = max(lista) if (maxi > value): getchange(value, lista[1:]) if (maxi == value): cant_cambios.append(1) getchange(value, lista[1:]) if (maxi < value): for val in lista: new_val = value - val getchange(new_val, listasinmax(lista, val))`

def getWays(n, c): # Complete this function exchange = n c = sorted(c) values = reverse(c)

`getchange(exchange, values) return(sum(cant_cambios))`

trancekid + 1 comment thanks :P

amazinghacker + 0 comments [deleted]

subwaymatch + 0 comments Another thanks.

dramforever + 0 comments Can't thank you more...Just got wrong answer for a few test cases and saw your comment

ajcoo + 1 comment what is 64 bit int ? Is it long long ?

- RB
ronak319 + 2 comments int64_t ; declare like this . This one is portable and can be used on every machine whether it is 64 or 32 because size of long int and long long int depend on machines also but int64_t guarantee on every machine. In C++;

- AS
abhi15kk + 1 comment Thanks a lot.. I used long long int. It gave me WA for 3 cases.. Declaring it your way helped me pass those cases.. I don't know why it is not accepting long long int.

Saras_Arya + 1 comment Was it test case 9, 10, 14. I am getting WA for only those.

- AK
abhaykuma + 0 comments resolved ?

jimmyX + 0 comments thanks man, i bought two test cases for that xD

amazinghacker + 0 comments [deleted]tubededentifrice + 0 comments [deleted]- JL
quick_dudley + 0 comments That's weird: I was just using 32 bit ints and didn't have any problem.

kohei + 0 comments You saved me!! Thank you!

- S
sonicjoy2002 + 0 comments Damn! Should've read your comment before spending 5 hacko. Thank you.

nitinyadav + 0 comments Thank you

- H
HarryCho + 0 comments Additional info : Overflow occurs on the

`answer`

. Value of`N`

is in the range of`int`

(fairly small). - AM
maverick55 + 0 comments [deleted] royal146 + 0 comments Thanks

arunwizz + 0 comments thanks .. 3 cases were failing with jave int, java long worked..

suprchan + 0 comments Thank you :-). Failure to handle it costed 3 test cases!

Soumya_cbr + 2 comments Hi, How to switch to 64 bit int in c??? Please help... i am also facing the overflow issue

haha_ha + 0 comments declare long long int datatype instead of declaring int

- JM
juan_furattini + 0 comments - C: long long int
- C++: int64_t

Dikaios + 0 comments thanks

- GK
ShkKiit + 0 comments Had to buy testcases for that :P Wish i saw your comment haha

- G
guilin + 3 comments the only thing I learn is that alwasy use Long the only thing I learn is that alwasy use Long the only thing I learn is that alwasy use Long --repeat 3 times if it's very oimportant,重要的事情说三遍

- S
skybrown6543 + 3 comments **中国人出现了**。。（盟友）,+1s

- LF
fengli97 + 0 comments [deleted] rfeng2 + 2 comments 盟友+1

- FR
fasalirr + 2 comments - SL
love_lion_1 + 0 comments [deleted] wensi + 0 comments 盟友+1

- SL
love_lion_1 + 0 comments [deleted]

- SL
love_lion_1 + 0 comments 盟友+1

- ZL
zutianluo + 0 comments 喜相逢…

- LZ
h493263729 + 0 comments 你好 哈哈

bhanuchand + 0 comments [deleted]- ST
shengpu1126 + 0 comments Thank you very much for the tip! This is definitely something that programmers need to take care of: even though the algorithmic logic is right, be mindful of these details in practicality.

AnamikaAna + 0 comments I got all my test cases right after reading this comment :) Thanks a lot

Chuyinzki + 0 comments You are my hero.

- RK
hi_kommineni + 0 comments Thanks!!

- TP
tomasz_polgrabia + 0 comments I can only add my note: spending one hour on solving the only problem "integer overflow" (my algorithm was fine) without a possibility to look at the test cases makes my face ( ok, I will not embed any gifs, but you can imagine this) MAAAADDDDDDD. Task's author should warn at the beginning that the range of test cases' outputs exceed the normal range of the integer type.

abhinaw + 0 comments Even 64-bit int is not working for two test cases. Though it helped me in one test case.

First three test cases were not getting accepted. After using 64-bit int, two test cases are still not getting accepted.

- A
ajenal + 0 comments Saved my day.

- PG
parasgangwani292 + 0 comments thanks

- PP
Mumbaikar007 + 0 comments You made my day !!

- MK
kumar_malay888 + 1 comment this is my code and used java8 :- for coin change problem

public static String CoinChange(int input1,int[] input2) { return CoinChange(input1,input2,0,new HashMap()); }

`public static String CoinChange(int input1,int[] input2,int a, HashMap<String,Long> memo) { int b = 1; int c = 0; long d = 0; String x = ""; if(input1 == 0) { return (x = String.valueOf(b)); } if(a >= input2.length) { return (x = String.valueOf(c)); } String z = input1+"-"+a; if(memo.containsKey(z)) { d = memo.get(z); return (x = String.valueOf(d)); } int e = 0; long f = 0; while(e <= input1) { int g = input1 - e; f += CoinChange(input2, g, a+1,memo); e += input2[a]; } memo.put(z,f); return(x = String.valueOf(f)); and i was getting a error which is CandidateCode.java:43: error: incompatible types: int[] cannot be converted to int f += CoinChange(input2, g, a+1,memo); can anyone please solve this`

Yorunome + 0 comments f += CoinChange(input2, g, a+1,memo); You are missing an argument while passing the values here. Your first variable in the function definition is an int type and the second one is an array.

AkankshaRajhans + 1 comment This is my code in Java7 static long getWays(long n, long[] c){ // Complete this function long[] combinations=new long[(int)n]; combinations[0]=1; for(int j=0;j=c[j]) combinations[i]+=combinations[i-(int)c[j]]; } }

`return combinations[(int)n];`

} I am getting AArrayIndexOutOfBoundsException. Can anyone help ?

- SK
gk_2000 + 0 comments you can put code inside 3 backquotes in beginning and end to get formatting

- RW
rwan7727 + 3 comments Passed all cases:

int main() { int n; int m; cin >> n >> m; vector <long> c(m); for (int i=0;i<m;i++) cin >> c[i]; vector <long> numways(n+1); // numways[x] means # ways to get sum x numways[0]=1; // init base case n=0 // go thru coins 1-by-1 to build up numways[] dynamically // just need to consider cases where sum j>=c[i] for (int i=0;i<m;i++) { for (int j=c[i];j<=n;j++) { // find numways to get sum j given value c[i] // it consists of those found earlier plus // new ones. // E.g. if c[]=1,2,3... and c[i]=3,j=5, // new ones will now include '3' with // numways[2] = 2, that is: // '3' with '2', '3' with '1'+'1' numways[j]+=numways[j-c[i]]; } } cout << numways[n]; return 0; }

chammika + 0 comments This is the perfect answer ! Hats off!!

- JZ
jessica_0422 + 0 comments That makes sense, very clear! Thank you!

- YB
bishty472 + 0 comments i dont understand what j is.

- SM
mugu007 + 0 comments Thank You ... from 3 years in the Future.

- L
lancerchao + 3 comments A VERY helpful link for anyone who cannot understand the subproblems involved: http://www.ideserve.co.in/learn/coin-change-problem-number-of-ways-to-make-change

anonymouse_pal + 0 comments Thanks! That really helped! :)

ramdev64 + 0 comments Thanks. that was helpful :)

- RT
Rav1T3ja + 0 comments Good site for many Important Algorithms. Good contribution lancerchao

mcervera + 3 comments The solution to this problem is a good example of an efficient and tight Dynamic Programming algorithm. For those of you who are struggling with it, here's a tip. The number of ways you can make change for

**n**using only the first**m**coins can be calculated using:(1) the number of ways you can make change for

**n**using only the first**m-1**coins.(2) the number of ways you can make change for

**n-C[m]**using the first**m**coins (where**C**is the array of coins).This data allows you to build the recurrence relation.

ricktetstall + 0 comments [deleted]- SP
suraj23patel + 1 comment Can anyone please explain how is this recurresnce derived?

mcervera + 1 comment Posting the recurrence would be the same as posting the solution to the problem since it is the only thing that you need to do to solve it.

Hint: define a bidimensional table. Each (m,n) position stores the number of ways you can obtain the amount "n" using only the first "m" coins. Define the boundary conditions and iteratively fill the table. The solution to the problem will be stored in position (M,N).

- ZX
zxnzysj + 0 comments thanks for your hint

- YW
YeahKey + 0 comments Your hint really helped~ Thx

Tsukuyomi1 + 7 comments Keeping it simple :D

n, m = map(int,input().split()) coins = list(map(int,input().split())) dp = [1]+[0]*n for i in range(m): for j in range(coins[i], n+1): dp[j]+=dp[j-coins[i]] print(dp[-1])

multidynamic + 0 comments 👍

- CW
sothisislife101 + 1 comment I've seen a lot of solutions similar to this one in other languages, but this is the first one I've seen in python. My solution is much more verbose, and I'm having difficulty understanding what exactly is going on here. Can someone walk me through it?

Tsukuyomi1 + 0 comments dp[j] stores the number of solutions for j. For base case j=0, number of solutions is 1(not using any coin). Now in the for loop, i represents the number of coins you are using. Initially i=0, so you cannot create change for any j>0, hence dp[j]=0 for j>0. Now in each iteration you add a new coin and update the number of solutions for those j which have value not less than the value of ith coin. This update is just adding the number of solutions when we use the ith coin which is equal to the number of solutions of creating change for j-coins[i]. We are finally done when we use all the coins and so we print dp[n].

diego_amicabile + 1 comment Once you get around to understand it, this is a much better and straightforward solution IMHO. It is also more in line with the explanations given on Topcoder.

I would sum up the algorithm this way.

# Initialize an array of combination numbers for all money amounts from 0 until the target amount # For all coin values: # for all money values starting from the coin value up to the target amount: # get the number of combinations for the money value minus the coin value # add this to the number of combinations of the money value, because you can construct them just adding a coin to them

This is a more verbose version with descriptive variables, that allows you to see the algorithm in action:

import sys target_money, coin_count = input().strip().split(' ') target_money, coin_count = [int(target_money), int(coin_count)] coins = list(map(int, input().strip().split(' '))) combinations = [1] + [0] * target_money for coin_index in range(coin_count): print("Combinations before processing coin {}: {}".format(coins[coin_index], combinations), file=sys.stderr) for money in range(coins[coin_index], target_money + 1): if (combinations[money - coins[coin_index]]) > 0: print(" Combinations for {} added to combinations for {} : {} += {}".format(money - coins[coin_index], money, combinations[money], combinations[money - coins[coin_index]]), file=sys.stderr) combinations[money] += combinations[money - coins[coin_index]] print("Combinations after processing coin {}: {}".format(coins[coin_index], combinations), file=sys.stderr) print("Combinations for {} : {}".format(target_money, combinations[target_money]), file=sys.stderr) print(combinations[target_money])

Now for instance, for test case

10 4 2 5 3 6

it will show the process on stderr:

Combinations before processing coin 2: [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Combinations for 0 added to combinations for 2 : 0 += 1 Combinations for 2 added to combinations for 4 : 0 += 1 Combinations for 4 added to combinations for 6 : 0 += 1 Combinations for 6 added to combinations for 8 : 0 += 1 Combinations for 8 added to combinations for 10 : 0 += 1 Combinations after processing coin 2: [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1] Combinations before processing coin 5: [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1] Combinations for 0 added to combinations for 5 : 0 += 1 Combinations for 2 added to combinations for 7 : 0 += 1 Combinations for 4 added to combinations for 9 : 0 += 1 Combinations for 5 added to combinations for 10 : 1 += 1 Combinations after processing coin 5: [1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 2] Combinations before processing coin 3: [1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 2] Combinations for 0 added to combinations for 3 : 0 += 1 Combinations for 2 added to combinations for 5 : 1 += 1 Combinations for 3 added to combinations for 6 : 1 += 1 Combinations for 4 added to combinations for 7 : 1 += 1 Combinations for 5 added to combinations for 8 : 1 += 2 Combinations for 6 added to combinations for 9 : 1 += 2 Combinations for 7 added to combinations for 10 : 2 += 2 Combinations after processing coin 3: [1, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4] Combinations before processing coin 6: [1, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4] Combinations for 0 added to combinations for 6 : 2 += 1 Combinations for 2 added to combinations for 8 : 3 += 1 Combinations for 3 added to combinations for 9 : 3 += 1 Combinations for 4 added to combinations for 10 : 4 += 1 Combinations after processing coin 6: [1, 0, 1, 1, 1, 2, 3, 2, 4, 4, 5] Combinations for 10 : 5

- CL
tigerleapgorge + 0 comments This is great. Thank you explaining!

krozaine + 0 comments You are simply awesome!! Made my day :)

Yu_Zhao1 + 1 comment How could any human beeing possibly come to this solution on his own?

metamemelord + 0 comments It's all practice dude. THis question in specific, is pretty straight forward.

- N
nitishks007 + 0 comments Nice! if we change the order of loop ( i inside j) we will get all possible ways of getting the change. Basically 2,2,5 will counted differently from 2,5,2

- JW
wangjiehui11235 + 0 comments Thank you~

- KT
kittenintofu + 3 comments For someone new to coding, this is a very interesting question and I def learned quite a lot from it. I'm writing this post just to help myself remember it.

The core concept is:

calculator(coins, numToUse, sum) = calculator(coins, numToUse-1, sum) + calculator(coins, numToUse, sum-coins[numToUse-1]); basically saying for the current sum, it can and only can be achieved by two sub-cases:

(1). you use one less kind of coins, and still achevie this sum.

(2). you still use the same set of coins, but achieve (sum-oneCoinValue). This "oneCoinValue" is the value of the coin that you eliminates in the sub-case (1) And if you total these two cases up, you get what you want.

And the next step is to speed up your program. If you think about it, there are some potential redundent calculations-- for a lot of sub-cases, they ended up trying to calculate the same sum. And here, I made my first improvement, and it FAILED hardcore... i used an array of size sum, as a look up table. Have I calculated the current sum already, if so, don't do the recursive call, but return the value that was already saved in the table; if not do the recursive call, and save the final return value to the lookup table.

Apparently, that approach did NOT end up well. I couldn't even get the first two mini test cases passed. And then I realized, apparently, there are two important arguments that are passed in: sum AND the numToUse, which basically tells you how many types of coins that you are allowed to use to get the sum. So if you only use a one dimentioanl array to save the result, that's not enough. Using coins {1,2,3} to achieve the number 10 of course is not going to be the same as if you only allowed to use coins {1,2}. The lookup table needs to be 2D. And the major calculator function look like this:

`private static long calculator(int[] coins, int numToUse, int sum){ if( sum ==0 ){ return 1; } else if(sum < 0 ){ return 0; } else if (numToUse <= 0){ return 0; } else { if (done[sum][numToUse] == -7){ done[sum][numToUse] = calculator(coins, numToUse-1, sum) + calculator(coins, numToUse, sum-coins[numToUse-1]) ; } return done[sum][numToUse]; } } // -7 is just the value that I intialize the array done, I picked it becuase I like number 7, haha`

svadali2 + 0 comments thank you for that, even though you wrote it for yourself :)

- S
stanfordkansas + 1 comment With yr impelemetation:

-Time Complexity is O(2^m) because of all possible subsets of the given coins

-Space Complexity is O(nm)

Is it right?

aitrop + 1 comment Seems like an overly complicated recursive solution to me. Surprised if it passed runtime tests.

- P
pendell + 0 comments Others have downvoted the comment, and the reason why is that it is essentally the same solution as in the editorial. There are some additional test cases that the editorial doesn't have, but the logic is otherwise sound.

The reason it passes the runtime tests is because of the if (done[sum][numToUse] == -7) check. This ensures that we only compute solutions one time -- if we need the same solution again, we just use the previously computed solution.

- AL
ammar_lokh1234 + 0 comments Hey I have a javascript program done the same way as OP, however I'm getting a runtime time error with my "done" array. I'm new to JS so can somebody help me spot the issue?

function getWays(n, c, m, done){ // Complete this functionc var res=0; if(n==0) { return 1; } if(n<0) return 0; if(m<=0) return 0; else { // Here is where I get the error: TypeError: Cannot read property '2' of undefined if(done[n][m]==-1) done[n][m] = getWays(n-c[m-1],c,m, done) + getWays(n,c,m-1, done); return done[n][m]; } } function main() { var n_temp = readLine().split(' '); var n = parseInt(n_temp[0]); var m = parseInt(n_temp[1]); var done = []; c = readLine().split(' '); c = c.map(Number); for(var i=0;i<n;i++){ var col = []; for(var j=0;j<m;j++) { col[j] =-1; } done[i] = col; } // Print the number of ways of making change for 'n' units using coins having the values given by 'c' var ways = getWays(n, c, m-1, done); console.log(ways); }

ishtiaque_h + 1 comment There's a very good explanation from Gayle (author of Cracking the Coding Interview book) for this problem in YouTube. Watch: https://www.youtube.com/watch?v=sn0DWI-JdNA

- BT
gravitation + 0 comments Thanks

Nothing in this thread really helped me at all, but after watching one minute of this video I realized the error in my approach to the problem

akshaykabra + 3 comments the input format of the question is not clear/really_bad. Also no constraints are given.

PRASHANTB1984 + 2 comments That section got wiped out along with some other changes. We'll be restoring it in a while.

- RS
roshan_santhosh + 1 comment Its been 9 days already. When will t be changed? The format is very unclear.

PRASHANTB1984 + 2 comments just updated it. Can you refresh?

akshaykabra + 0 comments Sir if number of coins to be taken would have been in input format it would be easier to take input.. can you please update the format??

JaskaranSingh + 1 comment What is the bound on M-the number of coins?

The input format isn't good.Right now the problem looks more of a challenge of input parsing rather than DP.

akshaykabra + 0 comments :D :D :D agree !! :)

amazinghacker + 1 comment [deleted]- JL
quick_dudley + 0 comments No, it's not necessary: I just used 2 loops.

amazinghacker + 0 comments [deleted]bossgates + 0 comments - Use long instead of int
- In some cases, Ci > N

- NA
nelson_a_antunes + 1 comment DP in python2 with 1D list/array:

value,_ = map(int,raw_input().strip().split(' ')) coins = map(int,raw_input().strip().split(' ')) ways = [0]*(value+1) for coin in coins: if coin > value: #if coin > value, there's no reason to use it continue ways[coin]+=1 #coin by itself as a set for i in range(coin+1,value+1): #fill the remaining sets with a new possibility with this coin ways[i] += ways[i-coin] print ways[value]

- RS
rswaghmare + 0 comments Wow! Did you develop this algorithm yourself?

hemendrav4 + 3 comments my c++ solution

int total,n; cin>>total>>n; int coins[n]; for(int i=0;i<n;i++) { cin>>coins[i]; } int T[n+1][total+1]; for(int i=0;i<=n;i++) { for(int j=0;j<=total;j++) { if(i==0) T[i][j]=0; else if(j==0) T[i][j]=1; else if(coins[i-1]>j) { T[i][j]=T[i-1][j]; } else T[i][j]=T[i-1][j]+T[i][j-coins[i-1]]; } } cout<<T[n][total]; return 0; }

- C
VARIK + 2 comments dont post your solution in the discussion please.

hemendrav4 + 0 comments okay

mohya + 0 comments do not read solution in discussion please

- GM
guilherme82 + 0 comments Could you please post the recursion solution?

Thanks

nandkishor8878 + 0 comments I am confused about choosing coins....

- Choose same coin again
- Choose another coin
- Not choose any coin

but you did not consider 2nd case I think..

Rohit_9083 + 1 comment import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { static long getWays(int amount, int[] coins){ long[] combinations = new long[amount + 1]; combinations[0] = 1; for(int coin : coins){ for(int i = 1; i < combinations.length; i++){ if(i >= coin){ combinations[i] += combinations[i - coin]; } } } return combinations[amount]; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int[] c = new int[m]; for(int c_i=0; c_i < m; c_i++){ c[c_i] = in.nextInt(); } // Print the number of ways of making change for 'n' units using coins having the values given by 'c' long ways = getWays(n, c); System.out.println(ways); } } perfect solution in java8...check out..must comment if u find better in java 8

Jabbar_Memon + 0 comments Good Solution..Easy to Understand :)

Sort 268 Discussions, By:

Please Login in order to post a comment