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));
}
}