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.