[Python] simple DFS + @cache (memoization)
python\nclass Solution:\n    def maxOperations(self, nums: List[int]) -> int:\n\n        @cache\n        def dfs(i, j, p):\n            if j - i + 1 < 2:\n     
```python\nclass Solution:\n    def maxOperations(self, nums: List[int]) -> int:\n\n        @cache\n        def dfs(i, j, p):\n            if j - i + 1 < 2:\n                return 0\n\n            m = 0\n            if nums[i] + nums[i + 1] == p:  # first two\n                m = max(m, dfs(i + 2, j, p) + 1)\n\n            if nums[j] + nums[j - 1] == p:  # last two\n                m = max(m, dfs(i, j - 2, p) + 1)\n\n            if nums[i] + nums[j] == p:  # first and last\n                m = max(m, dfs(i + 1, j - 1, p) + 1)\n\n            return m\n\n        return max(\n            dfs(2, len(nums) - 1, nums[0] + nums[1]),    # first two\n            dfs(0, len(nums) - 3, nums[-1] + nums[-2]),  # last two\n            dfs(1, len(nums) - 2, nums[0] + nums[-1]),   # first and last\n        ) + 1\n```
['Dynamic Programming', 'Recursion', 'Memoization']