Java Dynamic Planning: coin failover implementation code; java failover

Source: Internet
Author: User

Java Dynamic Planning: coin failover implementation code; java failover

The basic idea of dynamic planning is to break down the problem to be solved into several subproblems, first solve the subproblems, and save the solutions to these subproblems, if you need to solve these subproblems in the future, you can directly retrieve these computed solutions without repeated operations. You can use the table Filling Method to save the sub-problem, for example, saving it in an array.

A practical example is used to reflect the algorithm idea of Dynamic Planning-coin seeking.

Problem description:

Suppose there are several coins, and the number is infinite. Find the minimum number of coins that can constitute a certain number. For example, if several coins are [1, 3, 5], the minimum number of coins for the nominal value 2 is 2 (1, 1), and the minimum number of coins for the nominal value 4 is 2 (1, 3 ), the minimum number of coins for nominal value 11 is 3 (5, 5, 1 or 5, 3, 3 ).

Problem Analysis:

Assume that different groups of coins are the array coin [0 ,..., n-1]. calculate the minimum coin count (k) of the nominal value k. Then the count function and coin array coin meet the following conditions:

Count (k) = min (count (k-coin [0]),..., count (k-coin [n-1]) + 1;
The preceding formula is true only when k-coin [I]> = 0 & k-coin [I] <k.
Because of k-coin [I] <k, count (k) must satisfy count (I) (I <-[0, k-1]) known, therefore, backtracking is involved here.

So we can create a matrix [k + 1] [coin. length + 1], so that matrix [0] [j] is initialized to 0 values, while in matrix [I] [coin. length] minimum number of coins with a nominal value of I.

The specific process is as follows:

* k|coin 1  3  5  min  * 0    0  0  0  0  * 1    1  0  0  1  * 2    2  0  0  2  * 3    3  1  0  3, 1  * 4    2  2  0  2, 2  * 5    3  3  1  3, 3, 1  * 6    2  2  2  2, 2, 2  * ...

Finally, the specific Java code implementation is as follows:

Public static int backTrackingCoin (int [] coins, int k) {// backtracking + dynamic planning if (coins = null | coins. length = 0 | k <1) {return 0;} int [] [] matrix = new int [k + 1] [coins. length + 1]; for (int I = 1; I <= k; I ++) {for (int j = 0; j <coins. length; j ++) {int preK = I-coins [j]; if (preK>-1) {// only when not less than 0, the preK can exist in the array matrix for backtracking. matrix [I] [j] = matrix [preK] [coins. length] + 1; // the nominal value I is backtracking if (matrix [I] [coins. length] = 0 | matrix [I] [j] <matrix [I] [coins. length]) {// if the current number of coins is the least, update the min column's minimum number of coins matrix [I] [coins. length] = matrix [I] [j] ;}}} return matrix [k] [coins. length];}

The Code has been tested, and all test cases provided by the question have passed!

Summary

The above is all the content of the Code for implementing the coin-seeking problem of Java Dynamic Planning in this article. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.