C# recursive


  • 0
    H
    public bool PredictTheWinner(int[] nums) {
            return Recurse(nums, 0, nums.Length - 1, 0, 0, true);
        }
        
        public bool Recurse(int[] nums, int s, int e, int p1score, int p2score, bool turn) {
            int d = e - s;
            if (d == 0) {
                p1score += nums[s];
                if (turn && p1score >= p2score) return true;
                if (!turn && p1score > p2score) return true;
                return false;
            }
            
            if (!Recurse(nums, s + 1, e, p2score, p1score + nums[s], !turn)) return true;
            if (!Recurse(nums, s, e - 1, p2score, p1score + nums[e], !turn)) return true;
            
            return false;
        }
    

Log in to reply
 

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