Stone Game
LeetCode 877 · మధ్యస్థం · పరిష్కారం, intuition & దశలవారీ వాక్త్రూ
Minimax on (i, j, turn) — and with an even pile count Alice can always win.
నేను Stone Game (LeetCode 877) ను ఇలా పరిష్కరించాను — Game DP ప్యాటర్న్లోని ఒక మధ్యస్థం ప్రాబ్లమ్. కింద: intuition, దశలవారీ approach, నా accepted Python కోడ్, animated test cases, line-by-line debugger.
కింది వివరణ ఇంగ్లీష్లో ఉంది.
Intuition
Model the game as a score difference: Alice adds the pile she takes, Bob subtracts his. Alice maximises the difference, Bob minimises it, over the shrinking range of piles. Working it through shows Alice always wins with an even number of piles: she can choose to take all odd-indexed or all even-indexed piles, and one of those sets is larger.
Approach
- 1
dfs(i, j, turn) = Alice-minus-Bob score for the piles i..j.
- 2
Alice's turn: max of taking the left or right pile.
- 3
Bob's turn: min, with the pile value subtracted.
- 4
Alice wins if the full-range result is positive.
- 5
Final version uses the parity argument and returns True.
నేను ఎలా పరిష్కరించాను · మొదటి ఆలోచన నుంచి చివరి పరిష్కారం వరకు
lru_cache dfs(i, j, turn).
Alice always wins.
Minimax + memoPythonO(n²) time · O(n²) space
from functools import lru_cache
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
#Alice - Score positive - max_score we need
#Bob - Score negative - min score we need
n = len(piles)
@lru_cache(maxsize=None)
def dfs(i,j,turn):
if i>j:
return 0
if turn=="Alice":
from_left=piles[i]+dfs(i+1,j,"Bob")
from_right=piles[j]+dfs(i,j-1,"Bob")
return max(from_left,from_right)
else:
from_left=-piles[i]+dfs(i+1,j,"Alice")
from_right=-piles[j]+dfs(i,j-1,"Alice")
return min(from_left,from_right)
return True if dfs(0,n-1,"Alice")>0 else FalseMath argumentPythonO(1)
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
return TrueStone Game — తరచూ అడిగే ప్రశ్నలు
Stone Game వెనుక ఉన్న intuition ఏమిటి?
Model the game as a score difference: Alice adds the pile she takes, Bob subtracts his. Alice maximises the difference, Bob minimises it, over the shrinking range of piles. Working it through shows Alice always wins with an even number of piles: she can choose to take all odd-indexed or all even-indexed piles, and one of those sets is larger.
Stone Game (LeetCode 877) ను ఎలా పరిష్కరించాలి?
1. dfs(i, j, turn) = Alice-minus-Bob score for the piles i..j. 2. Alice's turn: max of taking the left or right pile. 3. Bob's turn: min, with the pile value subtracted. 4. Alice wins if the full-range result is positive. 5. Final version uses the parity argument and returns True.
Stone Game యొక్క time, space complexity ఎంత?
Time: O(1) final (O(n²) for minimax). Space: O(1). (Accepted approach: Math argument.)
Stone Game లో సాధారణ పొరపాట్లు ఏమిటి?
The 'return True' trick depends on an even pile count and an odd total — read the constraints before using it.
Stone Game ను పరిష్కరించడానికి ఒకటి కంటే ఎక్కువ మార్గాలు ఉన్నాయా?
Minimax + memo (O(n²) time · O(n²) space): lru_cache dfs(i, j, turn). Math argument (O(1)): Alice always wins.