|
2 | 2 | Alice and Bob play a game with piles of stones. There are an even number of piles arranged |
3 | 3 | in a row, and each pile has a positive integer number of stones piles[i]. |
4 | 4 |
|
5 | | - The objective of the game is to end with the most stones. The total number of stones across |
| 5 | + The objective of the game is to end with the most stones. The total number of stones across |
6 | 6 | all the piles is odd, so there are no ties. |
7 | 7 |
|
8 | | - Alice and Bob take turns, with Alice starting first. Each turn, a player takes the entire pile |
| 8 | + Alice and Bob take turns, with Alice starting first. Each turn, a player takes the entire pile |
9 | 9 | of stones either from the beginning or from the end of the row. This continues until there are |
10 | 10 | no more piles left, at which point the person with the most stones wins. |
11 | 11 |
|
12 | | - Assuming Alice and Bob play optimally, return true if Alice wins the game, or false if Bob wins. |
| 12 | + Assuming Alice and Bob play optimally, return true if Alice wins the game, or false if Bob wins. |
13 | 13 | |
14 | | - Ex: Input: piles = [5,3,4,5] |
15 | | - Output: true |
16 | | - Explanation: |
17 | | - Alice starts first, and can only take the first 5 or the last 5. |
18 | | - Say she takes the first 5, so that the row becomes [3, 4, 5]. |
19 | | - If Bob takes 3, then the board is [4, 5], and Alice takes 5 to win with 10 points. |
20 | | - If Bob takes the last 5, then the board is [3, 4], and Alice takes 4 to win with 9 points. |
21 | | - This demonstrated that taking the first 5 was a winning move for Alice, so we return true. |
| 14 | + Ex: Input: piles = [5,3,4,5] |
| 15 | + Output: true |
| 16 | + Explanation: |
| 17 | + Alice starts first, and can only take the first 5 or the last 5. |
| 18 | + Say she takes the first 5, so that the row becomes [3, 4, 5]. |
| 19 | + If Bob takes 3, then the board is [4, 5], and Alice takes 5 to win with 10 points. |
| 20 | + If Bob takes the last 5, then the board is [3, 4], and Alice takes 4 to win with 9 points. |
| 21 | + This demonstrated that taking the first 5 was a winning move for Alice, so we return true. |
22 | 22 |
|
23 | 23 | Space: O(n/2) |
24 | 24 | Time : O(1) |
|
0 commit comments