Alice and Bob play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones across all the piles is odd, so there are no ties.
Alice and Bob take turns, with Alice starting first. Each turn, a player takes the entire pile of stones either from the beginning or from the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alice and Bob play optimally, return true if Alice wins the game, or false if Bob wins.
Example 1:
Input: piles = [5,3,4,5] Output: true Explanation: Alice starts first, and can only take the first 5 or the last 5. Say she takes the first 5, so that the row becomes [3, 4, 5]. If Bob takes 3, then the board is [4, 5], and Alice takes 5 to win with 10 points. If Bob takes the last 5, then the board is [3, 4], and Alice takes 4 to win with 9 points. This demonstrated that taking the first 5 was a winning move for Alice, so we return true.
Example 2:
Input: piles = [3,7,2,3] Output: true
Constraints:
2 <= piles.length <= 500piles.length is even.1 <= piles[i] <= 500sum(piles[i]) is odd.This approach uses a straightforward observation: since Alice starts first and can always choose the better pile (since the number of piles is even and both play optimally), Alice will always have the advantage. Thus, Alice can always ensure she ends up with more stones than Bob by always picking the piles in a manner that maximizes her total stones. Therefore, the solution can be returned directly as true without sophisticated calculations.
In Python, this solution directly returns True as the outcome based on the problem constraints and the fact that Alice will always win with optimal moves.
Java
C
C++
C#
JavaScript
Time Complexity: O(1) - The solution involves a direct return statement.
Space Complexity: O(1) - No additional space is used.
The dynamic programming approach involves calculating the maximum stones a player can gather from any given state, thus determining the optimal strategy. We can define a DP table where dp[i][j] represents the maximum number of stones a player can get from piles i to j inclusive. The formula to update the DP table is determined by considering the take from left or right scenarios. In the end, Alice wins if dp[0][n-1] is greater than half of the total stones, ensuring her strategy leads to a win.
This Python solution defines a DP table to track maximum stones collected from segments. Through iteration, it populates the table considering optimal decisions at each step. Alice wins if she collects more stones, represented by a positive score in dp[0][n-1].
Java
C
C++
C#
JavaScript
Time Complexity: O(n^2) - We fill an n by n table.
Space Complexity: O(n^2) - Additional space for the table.
| Approach | Complexity |
|---|---|
| Greedy Approach | Time Complexity: O(1) - The solution involves a direct return statement. |
| Dynamic Programming Approach | Time Complexity: O(n^2) - We fill an n by n table. |
Stone Game - Leetcode 877 - Python • NeetCode • 34,440 views views
Watch 9 more video solutions →Practice Stone Game with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor