3040. Maximum Number of Operations With the Same Score II
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\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```\nclass Solution {\npublic:\n    int f(vector<int>& nums,int i,int j, int val,vector<vector<int>>& dp){\n        if(j-i<1) return 0;\n\n        if(dp[i][j]!=-1) return dp[i][j];\n        int maxVal = 0;\n        // cout<<"\\n";\n        // for(int x = i;x<=j;x++){\n        //     cout<<x<<" ";\n        // }\n        if(val>-1){\n            if((nums[i]+nums[i+1]) == val){\n                maxVal = max(maxVal,f(nums,i+2,j,val,dp)+1);\n            }\n            if((nums[j]+nums[j-1]) == val){\n                maxVal = max(maxVal,f(nums,i,j-2,val,dp)+1);\n            }\n            if((nums[i]+nums[j]) == val){\n                maxVal = max(maxVal,f(nums,i+1,j-1,val,dp)+1);\n            }\n        }\n        else{\n            maxVal = max(f(nums,i+2,j,nums[i]+nums[i+1],dp)+1,f(nums,i,j-2,nums[j]+nums[j-1],dp)+1);\n            maxVal = max(maxVal,f(nums,i+1,j-1,nums[i]+nums[j],dp)+1);\n        }\n        return dp[i][j] = maxVal;\n    }\n    int maxOperations(vector<int>& nums) {\n        vector<vector<int>> dp(nums.size()+1,vector<int>(nums.size()+1,-1));\n        int ans = f(nums,0,nums.size()-1,-1,dp);\n        return ans;\n    }\n};\n\n```
['C++']