-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path486. Predict the Winner.cpp
55 lines (32 loc) · 1.04 KB
/
486. Predict the Winner.cpp
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
46
47
48
49
50
51
52
53
54
55
class Solution {
int helper(vector<int>& nums, int i, int j, vector<vector<int> > &dp) {
if (i > j || i > nums.size()-1 || j < 0)
return 0;
if (dp[i][j] != -1)
return dp[i][j];
int left = nums[i] + min(helper(nums, i+2, j, dp) , helper(nums, i+1, j-1, dp));
int right = nums[j] + min(helper(nums, i, j-2, dp), helper(nums, i+1, j-1, dp));
dp[i][j] = max(left, right);
return dp[i][j];
}
public:
bool PredictTheWinner(vector<int>& nums) {
int i = 0;
int j = nums.size()-1;
if(nums.empty())
return true;
if(nums.size() == 1)
return true;
if(nums.size() == 2)
return true;
vector<vector<int> > dp(nums.size(), vector<int> (nums.size(), -1));
long sum = 0;
for(auto n: nums)
sum +=n;
long playerone = helper(nums, i, j, dp);
if ( (sum - playerone) <= playerone)
return true;
else
return false;
}
};