VOOZH about

URL: https://www.geeksforgeeks.org/dsa/optimal-strategy-for-a-game-set-3/

⇱ Optimal Strategy for a Game | Set 3 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Optimal Strategy for a Game | Set 3

Last Updated : 12 Jul, 2025

Consider a row of n coins of values v1 . . . vn, where n is even. We play a game against an opponent by alternating turns. In each turn, a player selects either the first or last coin from the row, removes it from the row permanently, and receives the value of the coin. Determine the maximum possible amount of money we can definitely win if we move first. 
Also, print the sequence of moves in the optimal game. As many sequences of moves may lead to the optimal answer, you may print any valid sequence.

Before the sequence, the part is already discussed in these articles. 

  1. Optimal Strategy for a Game
  2. Optimal Strategy for a Game | Set-2

Examples: 

Input: 10 80 90 30 
Output: 110 RRRL
Explanation: 
P1 picks 30, P2 picks 90, P1 picks 80 and finally P2 picks 10.
Score obtained by P1 is 80 + 30 = 110 
Max possible score for player 1 is : 110 
Optimal game moves are : RRRL 

Input: 10 100 10 
Output: 20 RRL 
 

Approach: 
In each turn(except the last) a player will have two options either to pick the bag on the left or to pick the bag on the right of the row. Our focus is to evaluate the maximum score attainable by P1, let it be S. P1 would like to choose the maximum possible score in his turn whereas P2 would like to choose the minimum score possible for P1.
So P1 focuses on maximizing S, while P2 focuses on minimizing S.

Naive Approach:  

  • We can write a brute force recursive solution to the problem which simulates all the possibilities of the game and finds the score that is maximum under the given constraints.
  • The function maxScore returns the maximum possible score for player 1 and also the moves that take place through the course of the game.
  • Since the function needs to return both the maximum possible score and the moves that lead to that score, we use a pair of integer and string.
  • The string that represents the moves of the game consists of chars 'L' and 'R' meaning that the leftmost or the rightmost bag was picked respectively.

Below is the implementation of the above approach: 


Output: 
110 RRRL

 

The time complexity of the above approach is exponential.

Optimal Approach: 
We can solve this problem by using dynamic programming in time and space complexity.  

  • If we store the best possible answers for all the bags from some beginning i to some ending j then there can be at most such different subproblems.
  • Let dp(i, j) represent the max score P1 can attain if the only remaining bags in the row start from i and end at j. Then the following hold: 
    • if it is P1's turn 
      • dp(i, j) = maximum of score of bag i + dp(i+1, j) or score of bag j + dp(i, j-1).
    • if it is P2's turn 
      • dp(i, j) = minimum of dp(i+1, j) or dp(i, j-1)
        Since the current bag's score goes to P2 we don't add it to dp(i, j).
  • To keep the track of the moves that take place at a given state we keep an additional boolean matrix which allows us to reconstruct the entire game moves that lead to the maximum score.
  • The matrix leftBag(i, j) represents a state in which only the bag from i to j are present. leftBag(i, j) is 1 if it is optimal to pick the leftBag otherwise it is 0.
  • The function getMoves uses this matrix to reconstruct the correct moves.

Below is the implementation of the above approach: 


Output: 
110 RRRL

 

Time and space complexities of this approach are .
 

Comment