Line of Coins Game - aloalgo

Line of Coins Game

Medium

You are given a line of coins, each with a certain value. Two players take turns picking a coin from either end of the line. The player with the highest total value at the end wins. Assuming both players play optimally, determine the maximum value the first player can achieve.

Example 1

Input
[3, 2, 2, 3]
Output
5
Explanation:

If Player 1 picks 3 from the left, coins become [2, 2, 3]. Player 2 picks 3 from the right (to maximize their own score), coins become [2, 2]. Player 1 picks 2, coins become [2]. Player 2 picks 2. P1 total: 3+2=5. P2 total: 3+2=5. If Player 1 picks 3 from the right, coins become [3, 2, 2]. Player 2 picks 3 from the left, coins become [2, 2]. Player 1 picks 2, coins become [2]. Player 2 picks 2. P1 total: 3+2=5. P2 total: 3+2=5. In both cases, Player 1 gets a maximum of 5.

Example 2

Input
[5, 3, 7, 10]
Output
15
Explanation:

If Player 1 picks 10 (right), coins become [5, 3, 7]. Player 2 faces [5, 3, 7]. P2 picks 7 (right), coins become [5, 3]. P1 faces [5, 3]. P1 picks 5 (left), coins become [3]. P2 faces [3]. P2 picks 3. P1 total: 10 + 5 = 15. P2 total: 7 + 3 = 10. (Other optimal choices for P1 would lead to a lower score.)

Example 3

Input
[10, 20]
Output
20
Explanation:

Player 1 picks 20. Coins become [10]. Player 2 picks 10. Player 1's total is 20.

Loading...
Input
[3, 2, 2, 3]
Output
5

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!