[C++] Top down DP
Approach\n Describe your approach to solving the problem. \n- Base case:\n    - r - l + 1 < 2: 0\n- State transformation:\n    - op1Score: nums[l] + nums[l+1]\n
# Approach\n<!-- Describe your approach to solving the problem. -->\n- Base case:\n    - `r - l + 1 < 2`: `0`\n- State transformation:\n    - `op1Score`: `nums[l] + nums[l+1]`\n    - `op2Score`: `nums[r-1] + nums[r]`\n    - `op3Score`: `nums[l] + nums[r]`\n    - `op1`: op1Score == prev ? `1 + dp(prev, l+2, r)` : `0`\n    - `op2`: op2Score == prev ? `1 + dp(prev, l, r-2)` : `0`\n    - `op3`: op3Score == prev ? `1 + dp(prev, l+1, r-1)` : `0`\n    - `dp(prev, l, r) = max(op1, op2, op3)`\n\n# Complexity\n- Time complexity: O(n^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n^2)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int maxOperations(vector<int>& nums) {\n        int n = nums.size();\n        vector<vector<int>> memo(n, vector<int>(n, -1));\n        function<int(int,int,int)> dp = [&](int prev, int l, int r) {\n            if (r - l + 1 < 2)\n                return 0;\n            if (memo[l][r] != -1)\n                return memo[l][r];\n            int op1Score = nums[l] + nums[l+1];\n            int op2Score = nums[r-1] + nums[r];\n            int op3Score = nums[l] + nums[r];\n            int op1 = op1Score == prev ? 1 + dp(prev, l+2, r) : 0;\n            int op2 = op2Score == prev ? 1 + dp(prev, l, r-2) : 0;\n            int op3 = op3Score == prev ? 1 + dp(prev, l+1, r-1) : 0;\n            return memo[l][r] = max({op1, op2, op3});\n        };\n        return 1 + max({\n            dp(nums[0]+nums[1], 2, n-1),\n            dp(nums[n-2]+nums[n-1], 0, n-3),\n            dp(nums[0]+nums[n-1], 1, n-2),\n        });\n    }\n};\n```
['Dynamic Programming', 'Memoization', 'C++']