WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Dynamic Programming · Advanced · question 44 of 100

Implement the solution to the Optimal Strategy for a Game problem using dynamic programming.?

📕 Buy this interview preparation book: 100 Dynamic Programming questions & answers — PDF + EPUB for $5

The Optimal Strategy for a Game problem is a classic problem in dynamic programming. The problem statement is as follows:

Given an array of integers representing the values of coins in a game, two players take turns choosing a coin from one of the ends of the array until all coins have been chosen. The player with the highest sum of coins at the end wins. Assuming both players play optimally, write a function to determine the maximum amount of money the first player can win.

The optimal substructure of this problem is as follows: To calculate the maximum amount of money the first player can win, we need to determine the maximum amount of money the first player can win given the array A[i..j] (i and j are the indices of the beginning and end of the array). We can calculate this recursively by considering two cases:

1. The first player chooses A[i]: In this case, the second player can choose A[i+1] or A[j]. If the second player chooses A[i+1], then the first player must choose between A[i+2] and A[j]. If the second player chooses A[j], then the first player must choose between A[i+1] and A[j-1]. In both cases, the first player wins the value of A[i] plus the maximum amount of money the first player can win given the remaining array. This can be expressed as:

    dp[i][j] = A[i] + min(dp[i+2][j], dp[i+1][j-1])

2. The first player chooses A[j]: In this case, the second player can choose A[i] or A[j-1]. If the second player chooses A[i], then the first player must choose between A[i+1] and A[j-1]. If the second player chooses A[j-1], then the first player must choose between A[i] and A[j-2]. In both cases, the first player wins the value of A[j] plus the maximum amount of money the first player can win given the remaining array. This can be expressed as:

    dp[i][j] = A[j] + min(dp[i+1][j-1], dp[i][j-2])

The base case is when i=j, in which case there is only one coin left, and the first player wins the value of that coin.

Using this recursive formula, we can construct the solution using dynamic programming. The time complexity of this DP solution is O(n2) and the space complexity is also O(n2). Here is the Java code that implements this DP solution:

public static int optimalStrategy(int[] A) {

    int n = A.length;

    int[][] dp = new int[n][n];

    for (int i = 0; i < n; i++) {
        dp[i][i] = A[i];
    }

    for (int len = 2; len <= n; len++) {
        for (int i = 0; i < n - len + 1; i++) {
            int j = i + len - 1;
            if (A[i] >= A[j]) {
                dp[i][j] = A[i] + (i + 1 <= j - 1 ? Math.min(dp[i+2][j], dp[i+1][j-1]) : 0);
            } else {
                dp[i][j] = A[j] + (i <= j - 2 ? Math.min(dp[i+1][j-1], dp[i][j-2]) : 0);
            }
        }
    }

    return dp[0][n-1];
}

Let’s test this function with an example:

int[] A = {3, 1, 7, 10};
int result = optimalStrategy(A);
System.out.println(result); // Output: 13

In this example, the optimal strategy for the first player is to choose the coin with value 3, then the coin with value 10. The second player can then only choose the coin with value 7. So, the first player wins 3 + 10 = 13.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Dynamic Programming interview — then scores it.
📞 Practice Dynamic Programming — free 15 min
📕 Buy this interview preparation book: 100 Dynamic Programming questions & answers — PDF + EPUB for $5

All 100 Dynamic Programming questions · All topics