Two players are playing a card game where the deck of cards are layed out in a straight line and each card value is visible to both the players.
The value of each card can range from a reasonable [-ve to +ve] number and the length of deck in n.
The rules of the game are such:
Return the maximum sum of cards Player 1 can obtain by playing optimally.
Example 1:
Input: cards = [1, 2, -3, 8]
Output: 3
Explanation:
Turn 1: Player 1 picks the first 2 cards: 1 + 2 = 3 points
Turn 2: Player 2 gets the rest of the deck: -3 + 8 = 5 pointsExample 2:
Input: cards = [1, 1, 1, 1, 100]
Output: 101
Explanation:
Turn 1: Player 1 picks cards[0] = 1 point
Turn 2: Player 2 picks cards[1] + cards[2] + cards[3] = 3 points
Turn 3: Player 1 picks cards[4] = 100 pointspublic int maxScore(int[] cards) {
// todo
}Can you do it with O(1) space?
Related problems: