1198. Stone Game I

MediumDynamic ProgrammingInterval DPGame Theory

Piles of stones are arranged in a row (an even number of piles, odd total). Two players alternately take a whole pile from either end, and the player with more stones wins. Assuming optimal play, return true if the first player wins. The input is JSON {piles}.

Input: JSON {piles}.

Output: Boolean — true if the first player wins.

Examples

Example 1
Input: {"piles":[5,3,4,5]}
Output: true
Explanation: First player can force a win.
Example 2
Input: {"piles":[3,7,2,3]}
Output: true
Explanation: First player wins.
Example 3
Input: {"piles":[1,2]}
Output: true
Explanation: Take the larger pile.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →