Java 7-line DP solution


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

Log in to reply
 

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