909. Stone Game
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
Method Explanation
This problem can be solved using dynamic programming, but a much simpler and more elegant solution exists based on a mathematical insight. We will discuss both approaches.
Solution
Stone Game in Python
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
# This is a game theory problem. With the given constraints, Alice always wins.
# Let's analyze the game based on the parity of pile indices (0-indexed).
# 1. The number of piles `n` is even. This means there are n/2 even-indexed piles
# (piles[0], piles[2], ...) and n/2 odd-indexed piles (piles[1], piles[3], ...).
# 2. The total sum of stones is odd. This means the sum of even-indexed piles
# is not equal to the sum of odd-indexed piles. One sum must be larger.
# 3. Alice, playing first, can choose her strategy. At the beginning, the ends
# are piles[0] (even index) and piles[n-1] (odd index).
# 4. Alice can decide to always take either even-indexed piles or odd-indexed piles.
# - If she wants the even-indexed piles (assuming their sum is larger), she takes piles[0].
# The remaining piles are piles[1...n-1]. Both ends are now odd-indexed.
# - Bob is forced to take an odd-indexed pile. After his move, the ends will be
# one even-indexed and one odd-indexed pile again.
# - This allows Alice to always take an even-indexed pile on her turn, while Bob is
# forced to take odd-indexed piles.
# Since Alice can choose at the start whether to collect all even-indexed piles or all
# odd-indexed piles, she can simply choose the group with the larger total sum.
# Therefore, Alice has a guaranteed winning strategy.
return True
Stone Game in Java
class Solution {
public boolean stoneGame(int[] piles) {
// This is a game theory problem. With the given constraints, Alice always wins.
// Let's analyze the game based on the parity of pile indices (0-indexed).
// 1. The number of piles `n` is even. This means there are n/2 even-indexed piles
// (piles[0], piles[2], ...) and n/2 odd-indexed piles (piles[1], piles[3], ...).
// 2. The total sum of stones is odd. This means the sum of even-indexed piles
// is not equal to the sum of odd-indexed piles. One sum must be larger.
// 3. Alice, playing first, can choose her strategy. At the beginning, the ends
// are piles[0] (even index) and piles[n-1] (odd index).
// 4. Alice can decide to always take either even-indexed piles or odd-indexed piles.
// - If she wants the even-indexed piles (assuming their sum is larger), she takes piles[0].
// The remaining piles are piles[1...n-1]. Both ends are now odd-indexed.
// - Bob is forced to take an odd-indexed pile. After his move, the ends will be
// one even-indexed and one odd-indexed pile again.
// - This allows Alice to always take an even-indexed pile on her turn, while Bob is
// forced to take odd-indexed piles.
// Since Alice can choose at the start whether to collect all even-indexed piles or all
// odd-indexed piles, she can simply choose the group with the larger total sum.
// Therefore, Alice has a guaranteed winning strategy.
return true;
}
}
Stone Game in C++
class Solution {
public:
bool stoneGame(vector<int>& piles) {
// This is a game theory problem. With the given constraints, Alice always wins.
// Let's analyze the game based on the parity of pile indices (0-indexed).
// 1. The number of piles `n` is even. This means there are n/2 even-indexed piles
// (piles[0], piles[2], ...) and n/2 odd-indexed piles (piles[1], piles[3], ...).
// 2. The total sum of stones is odd. This means the sum of even-indexed piles
// is not equal to the sum of odd-indexed piles. One sum must be larger.
// 3. Alice, playing first, can choose her strategy. At the beginning, the ends
// are piles[0] (even index) and piles[n-1] (odd index).
// 4. Alice can decide to always take either even-indexed piles or odd-indexed piles.
// - If she wants the even-indexed piles (assuming their sum is larger), she takes piles[0].
// The remaining piles are piles[1...n-1]. Both ends are now odd-indexed.
// - Bob is forced to take an odd-indexed pile. After his move, the ends will be
// one even-indexed and one odd-indexed pile again.
// - This allows Alice to always take an even-indexed pile on her turn, while Bob is
// forced to take odd-indexed piles.
// Since Alice can choose at the start whether to collect all even-indexed piles or all
// odd-indexed piles, she can simply choose the group with the larger total sum.
// Therefore, Alice has a guaranteed winning strategy.
return true;
}
};
Stone Game in TypeScript
function stoneGame(piles: number[]): boolean {
// This is a game theory problem. With the given constraints, Alice always wins.
// Let's analyze the game based on the parity of pile indices (0-indexed).
// 1. The number of piles `n` is even. This means there are n/2 even-indexed piles
// (piles[0], piles[2], ...) and n/2 odd-indexed piles (piles[1], piles[3], ...).
// 2. The total sum of stones is odd. This means the sum of even-indexed piles
// is not equal to the sum of odd-indexed piles. One sum must be larger.
// 3. Alice, playing first, can choose her strategy. At the beginning, the ends
// are piles[0] (even index) and piles[n-1] (odd index).
// 4. Alice can decide to always take either even-indexed piles or odd-indexed piles.
// - If she wants the even-indexed piles (assuming their sum is larger), she takes piles[0].
// The remaining piles are piles[1...n-1]. Both ends are now odd-indexed.
// - Bob is forced to take an odd-indexed pile. After his move, the ends will be
// one even-indexed and one odd-indexed pile again.
// - This allows Alice to always take an even-indexed pile on her turn, while Bob is
// forced to take odd-indexed piles.
// Since Alice can choose at the start whether to collect all even-indexed piles or all
// odd-indexed piles, she can simply choose the group with the larger total sum.
// Therefore, Alice has a guaranteed winning strategy.
return true;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.