Decide the Winner of Optimal End-Pile Stone Selection
Quick Overview
Decide the Winner of Optimal End-Pile Stone Selection. Two players alternately take an entire pile from either end of a row, with the first player moving first.
Decide the Winner of Optimal End-Pile Stone Selection
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
### Problem
Two players alternately take an entire pile from either end of a row, with the first player moving first. Both maximize their final stone total. The number of piles is even, every pile is positive, and the total number of stones is odd. Return whether the first player wins under optimal play.
```cpp
class Solution {
public:
bool stoneGame(vector<int>& piles);
};
```
### Examples
**Example 1:**
```text
Input: piles = [2,7,4,6]
Output: true
Explanation: Original odd-index piles total13 and even-index piles total6. The first player can secure all piles of the larger parity.
```
**Example 2:**
```text
Input: piles = [9,2]
Output: true
Explanation: The first player chooses9; the second receives2.
```
### Constraints
- `2 <= piles.length <= 500`, and the length is even.
- `1 <= piles[i] <= 500`.
- The sum of all pile values is odd.
### Contract considerations
Players take whole endpoint piles; they do not remove an arbitrary number of stones or an interior pile.
```hint Read the boundary condition
Consider the sums of the original even and odd positions and which player can force one parity.
```
### Boundary reminders
Players take whole endpoint piles; they do not remove an arbitrary number of stones or an interior pile.
Overview: Decide the Winner of Optimal End-Pile Stone Selection. Two players alternately take an entire pile from either end of a row, with the first player moving first.
Decide the Winner of Optimal End-Pile Stone Selection
Google
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0
Problem
Two players alternately take an entire pile from either end of a row, with the first player moving first. Both maximize their final stone total. The number of piles is even, every pile is positive, and the total number of stones is odd. Return whether the first player wins under optimal play.
class Solution {
public:
bool stoneGame(vector<int>& piles);
};
Examples
Example 1:
Input: piles = [2,7,4,6]
Output: true
Explanation: Original odd-index piles total13 and even-index piles total6. The first player can secure all piles of the larger parity.
Example 2:
Input: piles = [9,2]
Output: true
Explanation: The first player chooses9; the second receives2.
Constraints
2 <= piles.length <= 500
, and the length is even.
1 <= piles[i] <= 500
.
The sum of all pile values is odd.
Contract considerations
Players take whole endpoint piles; they do not remove an arbitrary number of stones or an interior pile.
Boundary reminders
Players take whole endpoint piles; they do not remove an arbitrary number of stones or an interior pile.