Recently I am familiar with the dynamic programming method. This method is an algorithm design strategy for solving the optimization problem. The optimal value of the original problem can be decomposed into sub-structures, and the sub-structure is also optimal. This optimal sub-structure is similar to the greedy method. Of course, there will be an essential difference. The greedy method uses the optimal sub-structure in the top-down mode, the greedy method will first make a choice that does not seem optimal at the time, and then solve a subproblem of the result. The dynamic programming method uses sub-structures from the bottom up. He first finds the optimal solution of the sub-problem and then makes the choice. At the same time, in the dynamic planning method, the replay problem is stored. A common problem is divided into two values. In the process of the two values, the next value problem may have duplicate values. Next I wrote the implementation of finding a coin with a large face value.
package dynamicplanforcoinchange;/** * @author leon(liangzou0318@gmail.com) * @date 2012-11-22 * @filaname CoinsChangeimpl.java */public class CoinsChangeimpl { public static int makechanges(int money,int[] valuekinds){ int mincoins=money; for(int i=0;i<valuekinds.length;i++){ if(money<valuekinds[i]){ continue; } else{ int temp=makechanges(money-valuekinds[i],valuekinds)+1; if(mincoins>temp){ mincoins=temp; } } }return mincoins; }public static void main(String[] args) {int[] valuekinds=new int[]{25,21,10,5,1};int money=63;int a=makechanges(money,valuekinds);System.out.println(a);}}
Running result: 3