FastPrepCoin Game From The Ends
Problem · Dynamic Programming

Coin Game From The Ends

Learn this problem
MediumTekion logoTekionFULLTIMEPHONE SCREEN

Problem statement

You are given an even-length integer array coins representing a row of coin values.

Two players take turns. On a turn, a player must take the coin at the current left end or the current right end of the remaining row. The first player starts. Both players play optimally to maximize their own total.

Return true if the first player can obtain a total at least as large as the second player's total, and false otherwise.

What the interview report shared

The report described an even-length coin row where players alternately take a coin from either end, A moves first, and the player with the higher total wins.

Function

firstPlayerWins(coins: int[]) → boolean

Examples

Example 1

coins = [20,30,2,2,2,10]return = true

The six coins sum to 66. Optimal play lets the first player collect 42 and the second collect 24, so the first player wins.

Example 2

coins = [8,15,3,7]return = true

Taking 7 first forces the later choice of 15. The first player totals 22 and the second totals 11.

Constraints

  • 2 <= coins.length <= 500.
  • coins.length is even.
  • 1 <= coins[i] <= 10^6.

More Tekion problems

drafts saved locally
public boolean firstPlayerWins(int[] coins) {
  // Write your code here.
}
coins[20,30,2,2,2,10]
expectedtrue
checking account