Extremely easy Java recursive solution


  • 0
    S
    public boolean PredictTheWinner(int[] nums) {
        return canWin(0,0,0,nums.length-1, nums, true);
    }
    
    boolean canWin(int player1, int player2, int start, int end, int[] nums, boolean isPlayer1) {
        if(start > end) {
            return player1 >= player2 ? true : false;
        }
        if(isPlayer1) {
            return canWin(player1+nums[start], player2, start+1, end, nums, false) || canWin(player1+nums[end], player2, start, end-1, nums, false);
        } else {
            return canWin(player1, player2+nums[start], start+1, end, nums, true) && canWin(player1, player2+nums[end], start, end-1, nums, true);
        }
    }

Log in to reply
 

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