Leetcode 486. Predict the Winner Predict winner Problem Solving report

Source: Internet
Author: User
1 ideas for solving problems

It's school, and I'm back.
The problem is to give an array of two people to play, each person can select a number in the head or tail of the current array, as their score, and then the substitution, the number has been selected can not be used.
Now there are two of people playing, Player1 and Player2, asked to give the current array after Player1 can guarantee victory.

Problem-solving method is to make a decision, see whether there is a Player1 decision to win, this only need to pass down the election is good.
The so-called win, is the current election to the score, greater than the opponent can choose to. 2 Original question

Given an array of scores that are non-negative integers. Player 1 Picks one of the numbers from either end of the array followed by the Player 2 and then player 1 and. Each time a player is picks a number, that number is not is available for the next player. This continues until the scores have been chosen. 
The player with the maximum score wins. Given an array of scores, predict whether player 1 is the winner. 
You can assume each player plays to maximize his score. 
Example 1:input: [1, 5, 2] Output:false explanation:initially, Player 1 can choose between 1 and 2. If He is chooses 2 (or 1), then Player 2 can choose from 1 (or 2) and 5. 
If Player 2 chooses 5, then player 1 'll be left with 1 (or 2). 
So, final score of player 1 are 1 + 2 = 3, and Player 2 is 5.

Hence, Player 1 'll never be the winner and your need to return False. Example 2:input: [1, 5, 233, 7] output:true Explanation:player 1-A-chooses 1. Then Player 2 have to choose between 5 and 7. No matter WHICH Number Player 2 Choose, Player 1 can choose 233.

Finally, Player 1 has more score (234) than Player 2 (a), so you need to return True representing Player1 can win. 
Note:1 <= length of the array <= 20.
Any scores in the given array are non-negative integers and would not exceed 10,000,000. If the scores of both players are equal, then player 1 is still the winner.
3 AC Solution
public class Solution {public
    Boolean predictthewinner (int[] nums) {return
        helper (nums, 0, nums.length-1) >= 0;
    }

    private int Helper (int[] nums, int start, int end) {  
        if (start==end) {return
            Nums[start];
            Math.max (Nums[start]-helper (Nums,start+1,end), Nums[end]-helper (nums,start,end-1));
        }
    }

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.