c++ top down dp solution, original
Intuition\n Describe your first thoughts on how to solve this problem. \nwe can use 2d dp to track of front and tail of the current interval. we can use a prev_
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nwe can use 2d dp to track of front and tail of the current interval. we can use a prev_sum to make sure we can make the deletion. an elegant base case is when i>=j (when front pointer equals to or greater than the tail pointer) since we are required to have at least 2 element in the vector.  \n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\n    int dp[2001][2001];\npublic:\n    int maxOperations(vector<int>& nums) {\n        // delete 2 front\n        // delte 2 tail\n        // delete 1 front 1 tail\n\n        // maximize number of operations, not total score \n        // 2d dp with front and tail poiter\n        memset(dp, -1, sizeof(dp));\n        int n = nums.size();\n        return solve(nums, 0, n-1, 0, dp);\n\n    }\n    int solve(vector<int>& nums, int i, int j, int prev_sum, int dp[2001][2001]){\n        if(i >= j) return 0;\n        if(dp[i][j] != -1) return dp[i][j];\n        int result = 0;\n        // delete front;\n        \n        int front_sum = nums[i] + nums[i+1];\n        if(front_sum == prev_sum || prev_sum == 0){\n            //prev_sum = prev_sum == 0 ? front_sum \n            result = max(result, solve(nums, i+2, j, front_sum, dp) + 1);\n        }\n        \n        // delete end\n        int tail_sum = nums[j] + nums[j-1];\n        if(tail_sum == prev_sum || prev_sum == 0){\n            result = max(result, solve(nums, i, j-2, tail_sum, dp) + 1);\n        }\n\n        // delete both\n        int both_sum = nums[i] + nums[j];\n        if(both_sum == prev_sum || prev_sum == 0){\n            result = max(result, solve(nums, i+1, j-1, both_sum, dp) + 1);\n        }\n        return dp[i][j] = result;\n\n    }\n};\n```
['C++']