From f97796500ebd35c49cf0d38db3d10fef3e37c135 Mon Sep 17 00:00:00 2001 From: pradeeptosarkar <50446690+pradeeptosarkar@users.noreply.github.com> Date: Fri, 15 Sep 2023 14:26:32 +0000 Subject: [PATCH] Sync LeetCode submission - Stone Game V (cpp) --- LeetCode/problems/stone_game_v/solution.cpp | 48 +++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 LeetCode/problems/stone_game_v/solution.cpp 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