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 determine if Alice will always win if both Alice and Bob play optimally. Alice always goes first.
Both players take turns taking a pile of stones from either the beginning or the end of the row. The game continues until there are no more piles left. The player with the most stones wins.
Given an array piles
where piles[i]
is the number of stones in the i-th
pile, return true
if Alice wins the game, or false
if Bob wins the game. The game will always have an even number of piles, i.e., piles.length
is always even.
Given the constraints and the nature of the problem (both players playing optimally), a direct analysis reveals that Alice can always win if both play optimally when the number of piles is even.
n
piles when n
is even.For this problem, we can directly return true
since Alice will always win by following an optimal strategy.
class Solution {
public:
bool stoneGame(vector<int>& piles) {
// Alice can always win by playing optimally.
return true;
}
};
true
which is a constant time operation as there are no computations based on the input size.Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?