# JAVA 9 lines DP solution, easy to understand with improvement to O(N) space complexity.

• The dp[i][j] saves how much more scores that the first-in-action player will get from i to j than the second player. First-in-action means whomever moves first. You can still make the code even shorter but I think it looks clean in this way.

``````public boolean PredictTheWinner(int[] nums) {
int n = nums.length;
int[][] dp = new int[n][n];
for (int i = 0; i < n; i++) { dp[i][i] = nums[i]; }
for (int len = 1; len < n; len++) {
for (int i = 0; i < n - len; i++) {
int j = i + len;
dp[i][j] = Math.max(nums[i] - dp[i + 1][j], nums[j] - dp[i][j - 1]);
}
}
return dp[0][n - 1] >= 0;
}
``````

Here is the code for O(N) space complexity:

``````
public boolean PredictTheWinner(int[] nums) {
if (nums == null) { return true; }
int n = nums.length;
if ((n & 1) == 0) { return true; } // Improved with hot13399's comment.
int[] dp = new int[n];
for (int i = n - 1; i >= 0; i--) {
for (int j = i; j < n; j++) {
if (i == j) {
dp[i] = nums[i];
} else {
dp[j] = Math.max(nums[i] - dp[j], nums[j] - dp[j - 1]);
}
}
}
return dp[n - 1] >= 0;
}

``````

Edit : Since I have some time now, I will explain how I come up with this solution step by step:

1, The first step is to break the question into the sub-problems that we can program. From the question, the winning goal is that "The player with the maximum score wins". So one way to approach it is that we may want to find a way to maximize player 1's sum and check if it is greater than player 2's sum (or more than half of the sum of all numbers). Another way, after noting that the sum of all numbers is fixed, I realized that it doesn't matter how much player 1's total sum is as long as the sum is no less than player 2's sum. No matter how, I think we can easily recognize that it is a recursive problem where we may use the status on one step to calculate the answer for the next step. It is a common way to solve game problems. So we may start with using a brutal force recursive method to solve this one.

2, However, we always want to do better than brutal force. We may easily notice that there will be lots of redundant calculation. For example, "player 1 picks left, then player 2 picks left, then player 1 picks right, then player 2 picks right" will end up the same as "player 1 picks right, then player 2 picks right, then player 1 picks left, then player 2 picks left". So, we may want to use dynamic programming to save intermediate states.

3, I think it will be easy to think about using a two dimensional array dp[i][j] to save all the intermediate states. From step 1, we may see at least two ways of doing it. It just turned out that if we choose to save how much more scores that the first-in-action player will earn from position i to j in the array (as I did), the code will be better in a couple of ways.

4, After we decide that dp[i][j] saves how much more scores that the first-in-action player will get from i to j than the second player, the next step is how we update the dp table from one state to the next. Going back to the question, each player can pick one number either from the left or the right end of the array. Suppose they are picking up numbers from position i to j in the array and it is player A's turn to pick the number now. If player A picks position i, player A will earn nums[i] score instantly. Then player B will choose from i + 1 to j. Please note that dp[i + 1][j] already saves how much more score that the first-in-action player will get from i + 1 to j than the second player. So it means that player B will eventually earn dp[i + 1][j] more score from i + 1 to j than player A. So if player A picks position i, eventually player A will get nums[i] - dp[i + 1][j] more score than player B after they pick up all numbers. Similarly, if player A picks position j, player A will earn nums[j] - dp[i][j - 1] more score than player B after they pick up all numbers. Since A is smart, A will always choose the max in those two options, so:
dp[i][j] = Math.max(nums[i] - dp[i + 1][j], nums[j] - dp[i][j - 1]);

5, Now we have the recursive formula, the next step is to decide where it all starts. This step is easy because we can easily recognize that we can start from dp[i][i], where dp[i][i] = nums[i]. Then the process becomes a very commonly seen process to update the dp table. I promise that this is a very useful process. Everyone who is preparing for interviews should get comfortable with this process:
Using a 5 x 5 dp table as an example, where i is the row number and j is the column number. Each dp[i][j] corresponds to a block at row i, column j on the table. We may start from filling dp[i][i], which are all the diagonal blocks. I marked them as 1. Then we can see that each dp[i][j] depends only on dp[i + 1][j] and dp[i][j - 1]. On the table, it means each block (i, j) only depends on the block to its left (i, j - 1) and to its down (i + 1, j). So after filling all the blocks marked as 1, we can start to calculate those blocks marked as 2. After that, all blocks marked as 3 and so on.

So in my code, I always use len to denote how far the block is away from the diagonal. So len ranges from 1 to n - 1. Remember this is the outer loop. The inner loop is all valid i positions. After filling all the upper side of the table, we will get our answer at dp[0][n - 1] (marked as 5). This is the end of my code.

1. However, if you are interviewing with a good company, they may challenge you to further improve your code, probably in the aspect of space complexity. So far, we are using a n x n matrix so the space complexity is O(n^2). It actually can be improved to O(n). That can be done by changing our way of filling the table. We may use only one dimensional dp[i] and we start to fill the table at the bottom right corner where dp[4] = nums[4]. On the next step, we start to fill the second to the last line, where it starts from dp[3] = nums[3]. Then dp[4] = Math.max(nums[4] - dp[3], nums[3] - dp[4]). Then we fill the third to the last line where dp[2] = nums[2] and so on... Eventually after we fill the first line and after the filling, dp[4] will be the answer.

2. On a related note, whenever we do sum, subtract, multiply or divide of integers, we might need to think about overflow. It doesn't seem to be a point to check for this question. However, we may want to consider using long instead of int for some cases. Further, in my way of code dp[i][j] roughly varies around zero or at least it doesn't always increases with approaching the upper right corner. So it will be less likely to overflow.

• The goal is maximize the sum of first player.

We have a numeric array from `0 ~ n`, `i` and `j` is the index. `dp[i][j]` means the max sum we can get if the array starts from `i` and ends to `j`.

So `dp[i][i]` means only one coin and we pick firstly, `dp[i][i+1]` means there are two coins, we pick the larger one.

To maximum our gain, `dp[i][j] = max( nums[i] + dp[i + 1][j], dp[i][j - 1] + nums[j])`, since we either will pick the `i` or `j` coin. But here `dp[i + 1][j]` and `dp[i][j - 1]` are not the values directly available for us, it depends on the move that our opponent make.

Then we assume our opponent is as good as we are and always makes optimize move. The worse case is that we will get the minimal value out of all possible situation after our opponent makes its move.

So the correct dp formula would be
`dp[i][j] = max( min (dp[i + 1][j - 1], dp[i + 2][ j]) + v[i], min (dp[i][j - 2], dp[i + 1][ j - 1]) + v[j]})` .
If we pick `i`, then our opponent need to deal with subproblem `dp[i + 1][j]`, it either picks from `i + 2` or `j - 1`. Similarly, If we pick `j`, then our opponent need to deal with subproblem `dp[i][j - 1]` , it either pick from `i + 1` or `j - 2`. We take the worse case into consideration so use min() here.

``````    public boolean PredictTheWinner(int[] nums) {
int n = nums.length, sum = 0;
if(n % 2 == 0) return true;
int[][] dp = new int[n][n];
for(int i=0; i < n; i++) {
dp[i][i] = nums[i];
sum += nums[i];
}

for(int j = 0; j < n; j++){
for(int i = j - 1; i >= 0; i--){
int a = (i + 1 < n && j - 1 >= 0) ? dp[i + 1][j - 1] : 0;
int b = (i + 2 < n) ? dp[i + 2][ j] : 0;
int c = (j - 2 >= 0) ? dp[i][j - 2] : 0;
dp[i][j] = Math.max(Math.min(a, b) + nums[i], Math.min(a, c) + nums[j]);
}
}

return dp[0][n - 1] * 2 >= sum;
}``````

• @hot13399

Your comment intrigued me to give a more detailed explanation for my code. On the other hand, your notice that if the length of the array is even, player 1 always wins is brilliant. I cannot prove it but I feel that it is correct. I've upvoded your solution for that.

• Here is the code for O(N) space complexity:

``````public boolean PredictTheWinner(int[] nums) {
if (nums == null) { return true; }
int n = nums.length;
if ((n & 1) == 0) { return true; } // Improved with hot13399's comment.
int[] dp = new int[n];
for (int i = n - 1; i >= 0; i--) {
for (int j = i; j < n; j++) {
if (i == j) {
dp[i] = nums[i];
} else {
dp[j] = Math.max(nums[i] - dp[j], nums[j] - dp[j - 1]);
}
}
}
return dp[n - 1] >= 0;
}``````

• Given the induction rule requires `dp[i+1][j]` to calculate `dp[i][j]`, shouldn't we loop `i` descendingly rather than ascendingly?

``````    for (int len = 1; len < n; len++) {
for (int i = 0; i < n - len; i++) {
int j = i + len;
dp[i][j] = Math.max(nums[i] - dp[i + 1][j], nums[j] - dp[i][j - 1]);
}
}
``````

The reason why the ascending `i` works here is because of the preprocessing of `dp`, which, in my opinion, just happens to make it work. (And we have to inspect very carefully to verify this.)

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

Do you have some special considerations to pick up the ascending `i` that I miss? I'm pretty confused with this part.

Thanks

• This post is deleted!

• Given the induction rule requires `dp[i+1][j]` to calculate `dp[i][j]`, shouldn't we loop `i` descendingly rather than ascendingly?

``````    for (int len = 1; len < n; len++) {
for (int i = 0; i < n - len; i++) {
int j = i + len;
dp[i][j] = Math.max(nums[i] - dp[i + 1][j], nums[j] - dp[i][j - 1]);
}
}
``````

The reason why the ascending `i` works here is because of the preprocessing of `dp`, which, in my opinion, just happens to make it work. (And we have to inspect very carefully to verify this.)

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

Do you have some special considerations to pick up the ascending `i` that I miss? I'm pretty confused with this part.

Thanks

That is a good question. Actually we updated the dp table in the sequence of len = 0, 1, 2 ....
The case of len = 0 is our starting point. It is updated separately by " for (int i = 0; i < n; i++) { dp[i][i] = nums[i]; } ".
Taking dp[0][1] as an example, dp[0][1] depends on dp[0][0] and dp[1][1]. Both of them have been already calculated when len = 0;
Taking dp[1][4] as another example, len for dp[1][4] is 3 and dp[1][4] depends on dp[2][4] and dp[1][3]. Both of them have been calculated when len = 2.
Hopefully now you can see that for any point dp[i][j], the two points dp[i + 1][j] and dp[i][j - 1] have already been calculated in the previous loop. So, it doesn't matter if you loop i descending or ascending.

• dp[i][j] = max( min (dp[i + 1][j - 1], dp[i + 2][ j]) + v[i], min (dp[i][j - 2], dp[i + 1][ j - 1]) + v[j]})

This is more intuitive to understand than the posted solution. Good solution.

• @odin Does any one can prove this argument? I am quite interested in it.
If the number of the nums[] is even, the first play will always WIN.

• @Mr.Bin
Actually, this is quite straightforward. If you compute the sums of the odd elements and the even elements respectively, there will be 2 situations S_odd >= S_even or S_odd < S_even.
All the numbers are known to both players at the beginning, the 1st player has the priority to pick and he can easily figure out which sum is bigger. If S_even >= S_odd, the 1st player can always pick the first element (0 indexed) in the array, the 2nd player, however, has NO choice but pick either side and both are odd elements marked as 'O' in the original array. By repeating this process the 1st player will always win if we count tie is a win for the 1st player. Same thing if S_odd >= S_even as the first player just need to pick the last element in the array at the first move.
0 1 2 3 ... 2n - 2 2n-1 ( 2n elements)
E O E O ... E O

Hope this helps.

• @MichaelPhelps
Thank you, you saved my workday.
But can we use this approach for O(N) solution?

• @gorokhovsky
If N is even, the solution is straightforward and it is O(N).
If N is odd, you have to use dynamic programming to solve the above problem. It would be O(N^2).

• @MichaelPhelps
If N is even, the solution is O(1).
If N is odd we, at least, can check if max(nums[0], nums[N-1]) + min(even_sum, odd_sum) < max(even_sum, odd_sum) for O(N) and return false if it is true

Upd: The condition is too rare and using it appears ineffective

• @gorokhovsky we can turn this into a O(n) solution, can't we? Thus I think it should not be slow. Or maybe the test cases in OJ is too same?

• @gorokhovsky
Sorry for the typo it is O(1) time complexity.

But considering there is 50% chance you get an array with even elements. It is worth to put an if statement at the beginning.

Thanks for pointing out.

• @hot13399 here is a video from MIT talks about the same logic as yours: https://www.youtube.com/watch?v=Tw1k46ywN6E&feature=youtu.be&list=PLUl4u3cNGP6317WaSNfmCvGym2ucw3oGp&t=3622

• Good idea!many thanks for you

• Recursive solution:

``````private static boolean findWinner(int[] a, int i, int j, int p1, int p2, boolean flag) {
if (i == j) {
if (flag)
return p1 + a[i] >= p2;
return p1 >= p2 + a[i];
}
if (flag)
return findWinner(a, i + 1, j, p1 + a[i], p2, false) || findWinner(a, i, j - 1, p1 + a[j], p2, false);
return findWinner(a, i + 1, j, p1, p2 + a[i], true) && findWinner(a, i, j - 1, p1, p2 + a[j], true);

}
``````

• Similarly, if player A picks position j, player A will earn nums[j] - dp[i][j - 1] more score than player B after they pick up all numbers.

Hi odin, thanks for your answers. I have a quick question. Why it is "nums[j] - dp[i][j - 1]"? Is it possible to be "nums[j]+dp[i][j-1]" if A plays at first during [i, j-1]? Your reply will be appreciated very much!

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.