diff --git a/LeetCode/problems/stone_game_v/solution.cpp b/LeetCode/problems/stone_game_v/solution.cpp new file mode 100644 index 0000000..e11bafb --- /dev/null +++ b/LeetCode/problems/stone_game_v/solution.cpp @@ -0,0 +1,48 @@ +class Solution { +public: + + int dp[501][501]; + + int solve(vector& stoneValue, vector& 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;k0)? 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& stoneValue) + { + memset(dp, -1, sizeof dp); + + int n=stoneValue.size(); + + vector preSum(n); + preSum[0]=stoneValue[0]; + + for(int i=1;i