Guest User

recursive

a guest
Feb 11th, 2023
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     bool PredictTheWinner(vector<int>& a) {
  4.         const int n = (int)a.size();
  5.         int sm = 0;
  6.         for(int i = 0; i < n; i++) sm += a[i];
  7.         int dp[n][n][2];
  8.         memset(dp, -1, sizeof dp);
  9.         function<int(int, int, int)> f = [&](int l, int r, int t) {
  10.             if(l > r) return 0;
  11.             int& ans = dp[l][r][t];
  12.             if(ans != -1) return ans;
  13.             if(t) ans = min(f(l + 1, r, 1 - t), f(l, r - 1, 1 - t));
  14.             else ans = max(f(l + 1, r, 1 - t) + a[l], f(l, r - 1, 1 - t) + a[r]);
  15.             return ans;
  16.         };
  17.         int res = f(0, n - 1, 0);
  18.         return res >= sm - res;
  19.     }
  20. };
Add Comment
Please, Sign In to add comment