Algorithm-Coin Line II (Dynamic programming)

Source: Internet
Author: User

For a long time did not do the interview problem, feel their programming ability has become weaker.

Today, in the Lintcode to do a problem, about dynamic planning, more interesting.

Test instructions

A coin with n different values is lined up in a line. Two contestants took turns to take 1 or 2 coins in turn from the left until there were no coins. Calculate the total value of the coins that two people get, and those with high value win. Please decide whether the first player loses or wins? 

Examples:

Truefalse.

1. Solving ideas

We analyze this problem from the perspective of dynamic programming.

First of all, if only 1 or 2 coins, it must be the first player to win, this is not to be questioned.

An array, we analyze from the back forward (we assume that the subscript of the coin is I, Len is the length of the array), define a DP array, from I to the maximum value of Len.

1. Suppose i = Len, indicating that no coins are present, so dp[len] = 0;

2. Suppose i = len-1, that there is currently a coin can be taken, in order to ensure maximum, will certainly take, so dp[len-1] = values[len-1];

3. Suppose i = len-2, indicating that there are currently two coins to take, in order to ensure maximum, so two are taken, dp[len-2] = Values[len-2] + values[len-1]

4. Suppose i = len-3, that there are currently three coins to take, in order to ensure maximum, so two are taken (because only one word, another person is sure two, there may be bigger than us), dp[len-2] = Values[len-2] + values[len-1]

5. Suppose i = len-3, which means that there are currently three coins to take, which is divided into two cases:

A. Take only one coin, that is, at present, we take len-3 that coin, but did not say not to take after (that is the operation after the current I, we are here backwards derivation, so I have been calculated), only that the current only take len-3. Here comes the other way, the other is sure to try to get the most, it may take one or two, when it is to take one, we need to calculate Dp[i + 2], two, we need to calculate Dp[i + 3], thus making us the least (minimum), so at this time dp[i] = Values[i] + Math.min (Dp[i + 2], Dp[i + 3]).

B. Take two coins, which means take len-3 and len-2 two positions of coins. The same reason, dp[i] = Values[i] + values[i + 1] + math.min (dp[i + 3], Dp[i + 4]);

Finally, we're going to take the maximum value of both.

2. Code

1      Public BooleanFirstwillwin (int[] values) {2         if(Values.length = = 0) {3             return false;4         }5         if(Values.length <= 2) {6             return true;7         }8         intLen =values.length;9         intDp[] =New int[Len + 1];TenDp[len] = 0;//no coins to take . OneDp[len-1] = values[len-1];//only one can take it, definitely take it. ADp[len-2] = Values[len-2] + values[len-1];//only two can take them. -Dp[len-3] = values[len-3] + values[len-2];//only three can be taken, take the first two, as guaranteed maximum -          for(inti = len-4; I >= 0; i--) { theDp[i] = Math.max (Values[i] + math.min (dp[i + 2], Dp[i + 3]), -Values[i + 1] + Values[i + 2] + math.min (dp[i + 3], Dp[i + 4])); -         } -         intsum = 0; +          for(inti = 0; i < values.length; i++) { -Sum + =Values[i]; +         } A         intA = Sum-dp[0]; at         returnDp[0] >A; -}

Algorithm-Coin Line II (Dynamic programming)

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.