Easy-to-understand O(n) space Java solution


  • 0
    R
    public boolean PredictTheWinner(int[] nums) {
        if (nums == null || nums.length == 0) return true;        
        int n = nums.length;
        int[] scores = new int[n+1];
        for (int len = 0; len < n; len++) {
            for (int i = 0; i+len < n; i++) {
                scores[i] = Math.max(nums[i] - scores[i+1], nums[i+len] - scores[i]);
            }
        }
        return scores[0] >= 0;
    }

Log in to reply
 

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