JavaScript Solution


  • 0
    var PredictTheWinner = function(nums) {
        
    				    var n = nums.length - 1;
    				    
    
    				    if(SelectNumbers(nums, 0, 0) == true){return true;};
    				    if(SelectNumbers(nums, n, n) == true){return true;};
    
    				    if(SelectNumbers(nums, 0, n) == true){return true;};
    				    if(SelectNumbers(nums, n, 0) == true){return true;};
    
    				     
    				    return false;
    				    
    				};
    
    				var SelectNumbers = function(nums, i, j)
    				{ 
    					
    				    var n = nums.length - 1;
    
    				    if(n ==0 || n == 1){return true;}
    				    var player1 = nums[i];
    				    var player2 = 0;
    
    				    var k = 0;
    				    var P1Turn = false;
    
    				    var a = i; var b = j;
    				    while(k <= n)
    				    {
    
    	                	if(i === 0 && j === 0){k++; a = k; b = k;}
    	                	if(i === 0 && j !== 0){k++; n--; a = k; b = n;}
    	                	if(i !== 0 && j === 0){k++; n--; a = n; b = k;}
    	                	if(i !== 0 && j != 0){n--; a = n; b = n;}
    
    
    				        if(k === n)
    				        {
    				            if(P1Turn === true){ player1 += nums[k];}
    				            else{player2 += nums[k];}
    				            
    				            break;
    				        }
    				        else
    				        {
    				            
    			                if(P1Turn === true){ player1 += nums[a]; P1Turn = false;}
    			                else{player2 += nums[b]; P1Turn = true;}
    			               
    			           }
    			        }
    			 
    			    
    			    if(player1 >= player2){ return true; }
    			     
    			    return false;
    			    
    			};	
    

Log in to reply
 

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