Google | Onsite | Card Game
17916

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:

  1. Player 1 starts the game
  2. Each player has 3 option:
    (Option 1: Pick 1st card)
    (Option 2: Pick 1st two cards)
    (Option 3: Pick 1st three cards)
  3. You're only allowed to pick cards from the left side of the deck
  4. Both players have to play optimally.

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 points

Example 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 points
public int maxScore(int[] cards) {
	// todo
}
Follow-up:

Can you do it with O(1) space?


Related problems:

Comments (23)