Coin lines 395. Coin lines Ⅱ C ++,

Source: Internet
Author: User

Coin lines 395. Coin lines Ⅱ C ++,

394. Coin line I

Problem description: There are n coins lined up. Two contestants take one or two coins in turn from the right until there is no coin. The person who got the last coin wins. Are you sure you want to win or lose the first player?

Example: n = 1, return true.

N = 2, return true.

N = 3, false is returned.

N = 4, return true.

N = 5, return true.

Challenge: O (1) time complexity and O (1) storage.

Analysis: when there are only one or two coins, the first person wins, because the second person does not have to take it; when there are three coins, the first person will lose either one or two; when there are four coins, as long as the first person takes one coin, you can win. Of course, five coins, as long as you take two coins for the first time, will win, it can be seen that as long as the number of coins is a multiple of 3, the first player is required to lose, the reason is very simple. Game theory does not have the case of who is smart and stupid, so only when it is a multiple of 3, when the first player takes one coin, the second player takes two coins. When the first player takes two coins, the second player takes one coin, in this way, the second player can guarantee a multiple coin of 3, and the last time the coin is taken, it must be his own. In this case, the first player must lose. That's right, that's it. Okay, I used natural language to describe the game theory for the first time. In fact, I found the rule. Haha, don't laugh at me ......

 

class Solution {public:    /*     * @param n: An integer     * @return: A boolean which equals to true if the first player will win     */    bool firstWillWin(int n) {        // write your code here        if( n % 3 == 0){            return false;        }else{            return true;        }    }};
395. Coin line II

 

 

Problem description: There are n coins of different values in a line. Two contestants take one or two coins from the left in turn until there is no coin. Calculate the total value of coins obtained by two people respectively, and those with high values will win.

Are you sure you want to win or lose the first player?

Example:

Returns true if array A = [1, 2.

Returns false if array A = [1, 2, 4.

Analysis: first, if there are only one or two coins, I .e., len <= 2, then the first person takes all the coins and wins. Then we analyze other situations, the array getMax [I] is used to represent the maximum value that can be obtained from the first person in the I to the last person. For the Value Analysis of elements in the specific array, refer to the analysis of Daniel. After assigning the value to the array getMax, then, getMax [0] is the maximum value that the first person can obtain, cyclically calculates the sum of all coins, obtains the maximum value that the second person can obtain, and compares the two sizes, you can determine whether the first player wins or loses. The call... is finished, but I didn't write the key analysis...

 

class Solution {public:    /*     * @param values: a vector of integers     * @return: a boolean which equals to true if the first player will win     */    bool firstWillWin(vector
 
   &values) {        // write your code here        int len = values.size();        // vector
  
    getMax(len+1);        int getMax[len + 1];        if(len <= 2){            return true;        }        getMax[len] = 0;        getMax[len - 1] = values[len - 1];        getMax[len - 2] = values[len - 1] + values[len - 2];        getMax[len - 3] = values[len - 2] + values[len - 3];        for(int i = len - 4;i >= 0;i--){            int getMax1 = values[i] + (getMax[i+2] < getMax[i+3] ? getMax[i+2] : getMax[i+3]);            int getMax2 = values[i] + values[i+1] + (getMax[i+3] < getMax[i+4] ? getMax[i+3] : getMax[i+4]);            getMax[i] = getMax1 > getMax2 ? getMax1 : getMax2;        }        int sum = 0;        for(int a = 0;a < len;a++){            sum += values[a];        }        if(getMax[0] > (sum - getMax[0])){            return true;        }else{            return false;        }    }};
  
 

Okay. Finally, this article will refer to the differences between the Vector class and the array. If you need it, you can also see it. The summary is very clear. However, I personally think, if you do not need to regularly Delete and add operations, it is better to set the length to an array! In addition, the above Code uses vector GetMax (len + 1); and int getMax [len + 1]; no problem when defining arrays, but do not know why, do not specify the length, in this case vector GetMax; an error is returned. The error message is as follows:

Input

[1,2,2]
Expected answer
true
Error Message

Segmentation fault (core dumped)

Related Article

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.