Data Structures and Algorithms: Stone Game I, II, III, IV

beginner
15 min

Data Structures and Algorithms: Stone Game I, II, III, IV

Welcome to our comprehensive guide on the Stone Game series! This lesson is designed to help you understand the concept of the Stone Game, a popular problem in computer science that tests your understanding of dynamic programming and strategy. Let's dive in!

Introduction šŸŽÆ

The Stone Game is a series of four problems that involve two players, each taking turns to pick up a stone from a pile. The goal is to end up with a larger total number of stones. In this lesson, we'll explore the four versions of the Stone Game (I, II, III, IV) and understand their unique rules.

Stone Game I šŸ“

In the first version of the Stone Game, the piles of stones have an odd number of stones, and the player who starts can choose any pile. The game ends when all piles are empty. The player with the higher total score at the end wins.

python
# Stone Game I Python implementation def stoneGameI(stones): n = len(stones) dp = [0] * (n + 1) # dp[i] denotes the maximum score player 1 can have with the first i piles # Building the dp array from the last stone pile to the first for i in range(n - 1, -1, -1): for j in range(i + 1, n): # Skip the current pile dp[i] = max(dp[i], stones[i] + min(dp[j + 1]) - min(dp[i])) return dp[0] # Player 1's score

Stone Game II šŸ’”

In the second version, the piles have an even number of stones, and the player who starts can pick up either one or two stones from a pile. The game ends when all piles are empty. The player who cannot pick up any more stones loses.

python
# Stone Game II Python implementation def stoneGameII(stones): n = len(stones) dp = [[0] * 2 for _ in range(n)] # dp[i][j] denotes the score of player j when the game ends with the i-th pile # Base case: If the pile has only one stone, player 1 wins if the stone is odd and player 2 wins if it's even for i in range(n): if stones[i] == 1: dp[i][0] = 1 dp[i][1] = 0 elif stones[i] == 2: dp[i][0] = 0 dp[i][1] = 1 else: dp[i][0] = (stones[i] - 1) % 2 # The remaining stone must be odd for player 1 to win dp[i][1] = (stones[i] + 1) % 2 # The remaining stone must be even for player 2 to win for i in range(n - 1, -1, -1): # Building the dp array from the last stone pile to the first for j in range(2): # Possible states: player 1 or player 2 for k in range(i + 1, n): # Exclude the current pile dp[i][j] = max(dp[i][j], dp[k][not j]) # Maximize the score, considering both players' moves if stones[i] % 2 == 0: # If the current pile has an even number of stones dp[i][j] = max(dp[i][j], dp[k][j] + (stones[i] // 2)) # Add half of the current pile to player j's score return dp[0][0] # Player 1's score

Stone Game III šŸ“

In the third version, the piles have an odd number of stones, and the player who starts can only pick up the entire pile. The game ends when all piles are empty. The player with the higher total number of stones at the end wins.

python
# Stone Game III Python implementation def stoneGameIII(stones): n = len(stones) dp = [0] * (n + 1) # dp[i] denotes the maximum score player 1 can have with the first i piles # Building the dp array from the last stone pile to the first for i in range(n - 1, -1, -1): for j in range(i + 1, n): # Skip the current pile dp[i] = max(dp[i], dp[j] + stones[i]) return dp[-1] # Player 1's score

Stone Game IV šŸ’”

In the fourth version, the piles have an even number of stones, and the player who starts can pick up either one or two stones from a pile. The game ends when all piles are empty. The player who cannot pick up any more stones loses. Unlike Stone Game II, players can pick up stones from multiple piles in a single turn.

Solving Stone Game IV requires a slightly modified approach to Stone Game II, but it's beyond the scope of this lesson. We recommend exploring advanced dynamic programming resources to tackle this challenge.

Quick Quiz
Question 1 of 1

What is the main difference between Stone Game I and Stone Game III?

We hope you enjoyed learning about the Stone Game series! With a solid understanding of these problems, you're one step closer to mastering dynamic programming and gaining valuable problem-solving skills for your programming journey. Keep practicing and happy coding! šŸš€šŸ’»šŸ¤“