forked from Chayandas07/LeetCode-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1140. Stone Game II
32 lines (32 loc) · 1.01 KB
/
1140. Stone Game II
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public:
int helper(vector<int>& piles, int m, int startIndex, int sum, vector<vector<int>>& dp) {
int n = piles.size();
if(startIndex>=n) {
return 0;
}
if(dp[m][startIndex] != -1) {
return dp[m][startIndex];
}
int maxStones = 0;
int cumulativeSum = 0;
for(int x = 1; x<=2*m; x++) {
int index = startIndex + x - 1;
if(index>=n) {
break;
}
cumulativeSum += piles[index];
maxStones = max(sum - helper(piles, max(m,x), startIndex+x, sum - cumulativeSum, dp), maxStones);
}
return dp[m][startIndex] = maxStones;
}
int stoneGameII(vector<int>& piles) {
int m = 1, n = piles.size(), startIndex = 0;
int totalStones = 0;
for(int i=0;i<n;i++) {
totalStones += piles[i];
}
vector<vector<int>> dp(2*n, vector<int>(n,-1));
return helper(piles,m,startIndex,totalStones,dp);
}
};