https://leetcode.com/problems/predict-the-winner/↵
Please check this question out on leetcode and can anyone tell me why my code wont work?↵
↵
~~~~~↵
class Solution {↵
public booleanpredictWinnerstoneGame(int[] piles) {↵
int total=0;↵
for(int pile:piles)total+=pile;↵
int [][]dp=new int[piles.length][piles.length];↵
for(int []arr:dp)Arrays.fill(arr,-1);↵
int alice=recursion(0,piles.length-1,piles,true,dp);↵
int bob=total-alice;↵
return alice>=0;↵
}↵
public int recursion(int start,int end,int []piles,boolean alice,int [][]dp){↵
if(start>end){↵
return 0;↵
}↵
if(dp[start][end]!=-1)return dp[start][end];↵
if(alice){↵
int stTake=piles[start]+recursion(start+1,end,piles,false,dp);↵
int endTake=piles[end]+recursion(start,end-1,piles,false,dp);↵
return dp[start][end]=Math.max(stTake,endTake);↵
}↵
else{↵
int stTake=-piles[start]+recursion(start+1,end,piles,true,dp);↵
int endTake=-piles[end]+recursion(start,end-1,piles,true,dp);↵
return dp[start][end]=Math.min(stTake,endTake);↵
}↵
}↵
}↵
~~~~~↵
↵
Please check this question out on leetcode and can anyone tell me why my code wont work?↵
↵
~~~~~↵
class Solution {↵
public boolean
int total=0;↵
for(int pile:piles)total+=pile;↵
int [][]dp=new int[piles.length][piles.length];↵
for(int []arr:dp)Arrays.fill(arr,-1);↵
int alice=recursion(0,piles.length-1,piles,true,dp);↵
int bob=total-alice;↵
return alice>=0;↵
}↵
public int recursion(int start,int end,int []piles,boolean alice,int [][]dp){↵
if(start>end){↵
return 0;↵
}↵
if(dp[start][end]!=-1)return dp[start][end];↵
if(alice){↵
int stTake=piles[start]+recursion(start+1,end,piles,false,dp);↵
int endTake=piles[end]+recursion(start,end-1,piles,false,dp);↵
return dp[start][end]=Math.max(stTake,endTake);↵
}↵
else{↵
int stTake=-piles[start]+recursion(start+1,end,piles,true,dp);↵
int endTake=-piles[end]+recursion(start,end-1,piles,true,dp);↵
return dp[start][end]=Math.min(stTake,endTake);↵
}↵
}↵
}↵
~~~~~↵
↵