Coin Change (Dynamic Programming)

跟風遠走 提交于 2019-12-10 11:33:54

问题


We usually the following recurrence relation for the coin change problem: (P is the total money for which we need change and d_i is the coin available)

But can't we make it like this: (V is the given sorted set of coins available, i and j are its subscripts with Vj being the highest value coin given)

C[p,Vi,j] = C[p,Vi,j-1]     if Vj > p
          = C[p-Vj,Vi,j] + 1  if Vj <=p

Is there anything wrong with what I wrote? Though the solution is not dynamic but isn't it more efficient?


回答1:


Consider P = 6, V = {4, 3, 1}. You would pick 4, 1, 1 instead of 3, 3, so 3 coins instead of the optimal 2.




回答2:


What you've written is similar to the greedy algorithm that works only under certain conditions. (See - How to tell if greedy algorithm suffices for finding minimum coin change?).

Also, in your version you aren't actually using Vi within the recurrence, so it's just a waste of memory



来源:https://stackoverflow.com/questions/13075242/coin-change-dynamic-programming

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!