Dynamic Programming Method to achieve zero Java

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.