Skip to content

Commit

Permalink
Sync LeetCode submission - Stone Game V (cpp)
Browse files Browse the repository at this point in the history
  • Loading branch information
pradeeptosarkar committed Sep 15, 2023
1 parent b2259c6 commit f977965
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions LeetCode/problems/stone_game_v/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class Solution {
public:

int dp[501][501];

int solve(vector<int>& stoneValue, vector<int>& preSum, int i, int j)
{
if(i>=j)
return 0;

if(dp[i][j]!=-1)
return dp[i][j];

int ans=0;

for(int k=i;k<j;k++)
{
int first = preSum[k] - ((i>0)? preSum[i-1]:0);
int second = preSum[j]-preSum[k];

if(first==second)
ans = max(ans, max(first+solve(stoneValue, preSum, i,k), second+solve(stoneValue, preSum,k+1,j)));

else if(first<second)
ans=max(ans, first+solve(stoneValue, preSum, i,k));

else
ans=max(ans, second+solve(stoneValue, preSum, k+1,j));
}

return dp[i][j]=ans;
}

int stoneGameV(vector<int>& stoneValue)
{
memset(dp, -1, sizeof dp);

int n=stoneValue.size();

vector<int> preSum(n);
preSum[0]=stoneValue[0];

for(int i=1;i<n;i++)
preSum[i]=preSum[i-1]+stoneValue[i];

return solve(stoneValue, preSum, 0, n-1);
}
};

0 comments on commit f977965

Please sign in to comment.