Java Recursion + Memorization
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nRecursion + Memorizatio
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nRecursion + Memorization\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 { \n\n    public static HashMap<Integer,int[][]> topCache = new HashMap<Integer,int[][]>();\n\n    public int maxOperations(int[] nums) {\n        topCache.clear();\n        int start = 0;\n        int end = nums.length-1;\n\n        int first = nums[start];\n        int second = nums[start+1];\n        int last =  nums[end]; \n        int secondLast = nums[end-1];\n        \n        int A = first + second;\n        int B = last + secondLast;\n        int C = first + last;\n\n        topCache.put(A,new int[nums.length][nums.length]);\n        topCache.put(B,new int[nums.length][nums.length]);\n        topCache.put(C,new int[nums.length][nums.length]);\n\n        int resA = 1 + solve(nums,start+2,end,A,topCache.get(A));\n        int resB = 1 + solve(nums,start,end-2,B,topCache.get(B));\n        int resC = 1 + solve(nums,start+1,end-1,C,topCache.get(C));\n               \n        return Math.max(resA,Math.max(resB,resC));   \n    }\n\n    public int solve(int[] nums,int start,int end,int match,int[][] cache){\n\n        if(end-start < 1)\n            return 0;\n        \n        if(cache[start][end] != 0)\n            return cache[start][end];\n        \n        int first = nums[start];\n        int second = nums[start+1];\n        int last =  nums[end]; \n        int secondLast = nums[end - 1];\n\n        int A = first + second;\n        int B = last + secondLast;\n        int C = first + last;\n        \n        int res = 0;\n\n        if(A == match){\n            int tempRes = 1 + solve(nums,start+2,end,A,cache);\n            if(tempRes >= res){\n                res = tempRes;\n            }\n        }\n        if(B == match){\n            int tempRes = 1 + solve(nums,start,end-2,B,cache);\n            if(tempRes >= res){\n                res = tempRes;\n            }\n        }\n            \n        if(C == match){\n            int tempRes = 1 + solve(nums,start+1,end-1,C,cache);\n            if(tempRes >= res){\n                res = tempRes;\n            }\n        }\n\n        cache[start][end] = res;\n\n        return res;   \n    }\n}\n\n```
['Java']