-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
45 lines (40 loc) · 1.39 KB
/
Copy pathsolution.cpp
File metadata and controls
45 lines (40 loc) · 1.39 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution
{
public:
int maximumAmount(vector<int> &arr)
{
int n = (int)arr.size();
// dp[i][j] -> max amount current player can collect
// from subarray arr[i..j]
vector<vector<long long>> dp(n, vector<long long>(n, 0));
// gap = length - 1 of the current subarray
for (int gap = 0; gap < n; ++gap)
{
for (int i = 0; i + gap < n; ++i)
{
int j = i + gap;
if (i == j)
{
// Only one coin
dp[i][j] = arr[i];
}
else if (j == i + 1)
{
// Two coins: take the maximum one
dp[i][j] = max(arr[i], arr[j]);
}
else
{
// x, y, z are the future gains after opponent's move
long long x = (i + 2 <= j) ? dp[i + 2][j] : 0;
long long y = (i + 1 <= j - 1) ? dp[i + 1][j - 1] : 0;
long long z = (i <= j - 2) ? dp[i][j - 2] : 0;
long long pickLeft = arr[i] + min(x, y);
long long pickRight = arr[j] + min(y, z);
dp[i][j] = max(pickLeft, pickRight);
}
}
}
return (int)dp[0][n - 1];
}
};