You are given an array of piles in piles, where each value is the number of stones in one pile. The piles remain in their original order, and two players take turns removing exactly one pile from either the left or right end of the remaining array.
The first player moves first. Each player keeps the stones they remove, and both players play optimally. Return true if the first player finishes with more stones than the second player; otherwise, return false. The input guarantees that a tie cannot occur.
Example 1:
Input: piles = [1,2]
Output: true
Explanation: The first player takes the pile containing 2 stones and leaves 1 stone, so the first player wins.
Example 2:
Input: piles = [4,1,2,6]
Output: true
Explanation: The first player can choose an optimal sequence of end removals that produces a strictly larger total than the second player.
Example 3:
Input: piles = [8,1,5,3,2,6]
Output: true
Explanation: With optimal play, the first player secures more stones; the odd total ensures the result is not a tie.
Constraints:
2 <= piles.length <= 500piles.length is even1 <= piles[i] <= 500The sum of all values in piles is oddOn each turn, a player removes exactly one pile from the left or right end of the remaining pilesNeed help or want more practice?