-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1690.cpp
More file actions
22 lines (17 loc) · 684 Bytes
/
Copy path1690.cpp
File metadata and controls
22 lines (17 loc) · 684 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
static int dp[1001][1001];
static int solve(const vector<int> &stones, int i, int j, int sum) {
if (i > j) return 0;
if (dp[i][j] != -1) return dp[i][j];
const int a = sum - stones[i] - solve(stones, i + 1, j, sum - stones[i]);
const int b = sum - stones[j] - solve(stones, i, j - 1, sum - stones[j]);
return dp[i][j] = max(a, b);
}
public:
Solution() { memset(dp, 0xFF, sizeof(dp)); }
int stoneGameVII(const vector<int> &stones) const {
const int sum = accumulate(begin(stones), end(stones), 0);
return solve(stones, 0, size(stones) - 1, sum);
}
};
int Solution::dp[1001][1001];