Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- bool PredictTheWinner(vector<int>& a) {
- const int n = (int)a.size();
- int sm = 0;
- for(int i = 0; i < n; i++) sm += a[i];
- int dp[n][n][2];
- for(int j = 0; j < n; j++) {
- for(int i = 0; i + j < n; i++) {
- for(int t: {0, 1}) {
- if(t) {
- dp[i][i + j][t] = min(
- (i + 1 > i + j? 0: dp[i + 1][i + j][1 - t]),
- (i > i + j - 1? 0: dp[i][i + j - 1][1 - t])
- );
- } else {
- dp[i][i + j][t] = max(
- (i + 1 > i + j? 0: dp[i + 1][i + j][1 - t]) + a[i],
- (i > i + j - 1? 0: dp[i][i + j - 1][1 - t]) + a[i + j]
- );
- }
- }
- }
- }
- int res = dp[0][n - 1][0];
- return res >= sm - res;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment