Welcome to another thrilling round of Alice and Bob's Stone Game! ๐ฎ
In this strategic battle, Alice and Bob face a row of stones, each carrying a specific point value (which can be positive, negative, or zero). The twist? Players must take stones from the beginning of the row, and they can grab 1, 2, or 3 stones per turn.
Game Rules:
- Alice always goes first
- Players alternate turns
- On each turn, a player must take 1, 2, or 3 stones from the front
- Each player's score = sum of values of stones they collected
- Both players play optimally (always make the best possible move)
Your mission: Determine who wins when both players play perfectly! Return "Alice" if Alice wins, "Bob" if Bob wins, or "Tie" if they end with equal scores.
Example: For stones [1,2,3,7], Alice takes 3 stones (1+2+3=6), Bob takes the last stone (7), Bob wins with 7 > 6.
Input & Output
Visualization
Time & Space Complexity
Each position is calculated exactly once, with O(1) work per position
Memoization cache stores result for each position, plus recursion stack
Constraints
- 1 โค stoneValue.length โค 5 ร 104
- -1000 โค stoneValue[i] โค 1000
- Both players play optimally