Straightforward JAVA Min-Max Solution


  • 0

    Recursive Min-Max solution to check if 1st user will win:

    public class Solution {
        public boolean PredictTheWinner(int[] nums) {
            return predictHelper(nums, 0, nums.length - 1, 0, 0, true);
        }
        public boolean predictHelper(int[] nums, int lo, int hi, int player, int opponent, boolean isFirst) {
            if (lo >= hi) {
                return isFirst && player >= opponent;
            }
            return !predictHelper(nums, lo + 1, hi, opponent, player + nums[lo], !isFirst) ||
                   !predictHelper(nums, lo, hi - 1, opponent, player + nums[hi], !isFirst);
        }
    }
    

Log in to reply
 

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