[Python3] DFS + Cache Solution, Clean & Concese
Approach\nUse depth-first search and try the three possible score (first two elements of nums, last two elements of nums, first and last elements of nums as the
# Approach\nUse depth-first search and try the three possible score (first two elements of `nums`, last two elements of `nums`, first and last elements of `nums` as the **score**.\n\nNote that this approach is essentially equivalent to dynamic programming.\n\n# Code\n```\nclass Solution:\n    def maxOperations(self, nums: List[int]) -> int:\n        @cache\n        def dfs(start, end, score):\n            if end - start + 1 < 2:\n                return 0\n            ans = 0\n            if nums[start] + nums[start + 1] == score:\n                ans = max(ans, 1 + dfs(start + 2, end, score))\n            if nums[end - 1] + nums[end] == score:\n                ans = max(ans, 1 + dfs(start, end - 2, score))\n            if nums[start] + nums[end] == score:\n                ans = max(ans, 1 + dfs(start + 1, end - 1, score))\n            return ans\n        n = len(nums)\n        return max(dfs(0, n - 1, nums[0] + nums[1]), dfs(0, n - 1, nums[-1] + nums[-2]), dfs(0, n - 1, nums[0] + nums[-1]))\n```
['Dynamic Programming', 'Depth-First Search', 'Python3']