Java 6ms solution


  • 0
    D

    dp[i][j] means how much player1 can outscore than player2 when considering i-th element to j-th element.

    public class Solution {
    '''public boolean PredictTheWinner(int[] nums) {
    ''''''int[][] dp = new int[nums.length+1][nums.length+1];
    ''''''for(int i = 1; i < dp.length-1; i++) {
    '''''''''dp[i][i+1] = Math.abs(nums[i]-nums[i-1]);
    ''''''}
    ''''''for(int d = 2; d <= dp.length-1; d++) {
    '''''''''for(int i = 1; i < dp.length-d; i++) {
    ''''''''''''int j = i + d;
    ''''''''''''dp[i][j] = Math.max(nums[j-1] - dp[i][j-1], nums[i-1] - dp[i+1][j]);
    '''''''''}
    '''''' }

        return dp[1][nums.length] >= 0;
    }
    

    }


Log in to reply
 

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