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.