<p>Given an array of integers called <code>nums</code>, you can perform <strong>any</strong> of the following operation while <code>nums</code> contains <strong>at least</strong> <code>2</code> elements:</p>

<ul>
	<li>Choose the first two elements of <code>nums</code> and delete them.</li>
	<li>Choose the last two elements of <code>nums</code> and delete them.</li>
	<li>Choose the first and the last elements of <code>nums</code> and delete them.</li>
</ul>

<p>The<strong> score</strong> of the operation is the sum of the deleted elements.</p>

<p>Your task is to find the <strong>maximum</strong> number of operations that can be performed, such that <strong>all operations have the same score</strong>.</p>

<p>Return <em>the <strong>maximum</strong> number of operations possible that satisfy the condition mentioned above</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,2,1,2,3,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> We perform the following operations:
- Delete the first two elements, with score 3 + 2 = 5, nums = [1,2,3,4].
- Delete the first and the last elements, with score 1 + 4 = 5, nums = [2,3].
- Delete the first and the last elements, with score 2 + 3 = 5, nums = [].
We are unable to perform any more operations as nums is empty.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,2,6,1,4]
<strong>Output:</strong> 2
<strong>Explanation:</strong> We perform the following operations:
- Delete the first two elements, with score 3 + 2 = 5, nums = [6,1,4].
- Delete the last two elements, with score 1 + 4 = 5, nums = [6].
It can be proven that we can perform at most 2 operations.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= nums.length &lt;= 2000</code></li>
	<li><code>1 &lt;= nums[i] &lt;= 1000</code></li>
</ul>

class Solution:
    def maxOperations(self, nums: List[int]) -> int:
        n = len(nums)
        if n < 2:
            return 0

        cache = {}

        target1 = nums[0] + nums[1]
        count1 = self.count_max_ops(nums, 2, n-1, target1, cache)

        target2 = nums[0] + nums[n-1]
        count2 = self.count_max_ops(nums, 1, n - 2, target2, cache)

        target3 = nums[n-1] + nums[n-2]
        count3 = self.count_max_ops(nums, 0, n - 3, target3, cache)

        return 1 + max(count1, count2, count3)


    def count_max_ops(self, nums: List[int], i: int, j: int, target: int, cache: dict) -> int:
        if i >= j:
            return 0

        if (i, j, target) in cache:
            return cache[(i, j, target)]

        max_count = 0
        if (nums[i] + nums[i + 1]) == target:
            count = 1 + self.count_max_ops(nums, i + 2, j, target, cache)
            max_count = max(max_count, count)
        
        if (nums[i] + nums[j]) == target:
            count = 1 + self.count_max_ops(nums, i + 1, j -1, target, cache)
            max_count = max(max_count, count)
        
        if (nums[j] + nums[j -1]) == target:
            count = 1 + self.count_max_ops(nums, i, j - 2, target, cache)
            max_count = max(max_count, count)

        # cache[(i, j)] = 1 + max_count     This is wrong, even if none of above "if" satisfies, this adds 1
        cache[(i, j, target)] = max_count
        return max_count
204243