Stone Game in JavaIn this game, stones are placed in a row (an input array is given). Two players are assigned the task of picking the stones of the maximum values. The player who collects stones of the maximum value wins the game. Player 1 will play first. After that, player two will play, and so on. At a time, a player can choose either 1 or 2 or 3 stones. The stones must be chosen from the left side only. Note that a player has to pick at least one stone. Our task is to decide which player will win the game if both the players play optimally. Example 1: int arr[] = {4, 3, 9, 16} Output: It is a tie between player 1 and player2 Explanation: Since stones are chosen from the left side and at max, only three stones are chosen, player 1 can only choose stones 1, 2 & 3. Thus, the total values of the stones chosen is 4 + 3 + 9 = 16. Now, it is the term of player 2. Player 2 can choose stone 4, which is of the value 16. Thus, both the players got the same value. Hence, it is a tie. Example 2: int arr[] = {8, 1, 1, 7} Output: Player 1 wins the game. Explanation: Player 1 plays optimally. Player 1 picks only the 1st stone from the left and leaves the rest. Thus, player 1 has a total value 8. Now, it is player 2 turn to pick the stones. Since players can only pick stones from the left only; therefore, player 2 has to pick stones of the values 1, 1, and 7. Thus, total value picked by the player 2 is 1 + 1 + 7 = 5, which is less than 8. Hence, player 1 wins. Example 3: int arr[] = {7, 2, 6, 3} Output: Player 2 wins the game. Explanation: To gain the maximum value, player 1 picks stones 1, 2, and 3. Thus, collecting the maximum value of 7 + 2 + 6 = 1. There is only one stone left which is picked by player 2. The stone is of the value 3. Thus, player 2 wins the game. Example 4: int arr[] = {1, 2, 0, 3, 6, 7, 4} Output: Player 1 wins the game. Explanation: To gain the maximum value, player 1 picks stones 1 and 2 only. Thus, collecting the maximum value of 1 + 2 = 3. Now, it is the turn of player 2, and the player 2 picks stones of the values 0, 3, and 6 (total values 0 + 3 + 6 = 9). Again, it is the turn of player 1, and player 1 picks 7 and 4. Thus, total value collected by the player 1 is 3 + 7 + 4 = 14, which is greater than 9. Hence, player 1 wins the game. Approach: Using RecursionIt is a brute force approach, where we consider each possible scenario for the player. The first scenario is when the player picks only one stone. The second scenario is when the player picks the two stones. The third scenario is when the player picks three stones. The maximum of these three scenarios is the value collected by the player. Observe the following program. FileName: StoneGame.java Output: For the stones of the following values: 4 3 9 16 It is a tie between player 1 and player 2 For the stones of the following values: 8 1 1 7 Player 1 wins the game. For the stones of the following values: 7 2 6 3 Player 2 wins the game. For the stones of the following values: 1 2 0 3 6 7 4 Player 1 wins the game. Complexity Analysis: The time complexity of the program is exponential as there are recursive calls for all the scenarios. The above program is not suitable for larger input as it will take a lot of time because of its high time complexity. Approach: Using Iteration (Dynamic Programming)We will assume that the dp[j] is the maximum value player 1 can get. Since player 1 can take 1 to 3 stones, player 2 can begin from either dp[j + 1], dp[j + 2], dp[j + 3]. Player 2 can also take 1 to 3 stones. Thus, resulting in the minimum of dp[j + 2 to 4], dp[j + 3 to 5], and dp[j + 4 to 6]. Thus, If any of the indexes are out of bounds, then it means the value is 0. The resulting score is stored dp[0]. FileName: StoneGame1.java Output: For the stones of the following values: 4 3 9 16 It is a tie between player 1 and player 2 For the stones of the following values: 8 1 1 7 Player 1 wins the game. For the stones of the following values: 7 2 6 3 Player 2 wins the game. For the stones of the following values: 1 2 0 3 6 7 4 Player 1 wins the game. Complexity Analysis: The time complexity of the program is O(3 * N). In terms of the asymptotic time complexity, it is O(N). As the program uses two auxiliary arrays for storing the answer and the sum of values of stones, the space complexity of the program is O(N + N), which is O(2 *N). In terms of asymptotic time, it is again O(N), where N is the total number of elements present in the stone array. The space complexity of the program can be further reduced. We can use variables instead of arrays, and it is evident in the following program. FileName: StoneGame1.java Output: For the stones of the following values: 4 3 9 16 It is a tie between player 1 and player 2 For the stones of the following values: 8 1 1 7 Player 1 wins the game. For the stones of the following values: 7 2 6 3 Player 2 wins the game. For the stones of the following values: 1 2 0 3 6 7 4 Player 1 wins the game. Complexity Analysis: The time complexity of the program is the same as the previous program. Since the program is only using variables and not arrays, the space complexity of the program is constant, i.e., O(1).
Next TopicTypeErasure in Java
