<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:
        if len(nums) < 2:
            return 0

        if len(nums) >= 2:

            unique_check = set(nums)
            if len(unique_check) == 1: 
                return len(nums) // 2 
            self.records = dict()
            result = self.aux_marker(nums, 0, len(nums)-1)
            if result:
                return max(result.values())
            else: 
                return 0


    def aux_marker(self, nums, i, j):
        # check if it's in the records
        identifier = f"{i}-{j}"
        if identifier in self.records:
            return self.records[identifier]

        if j - i == 1: 
            result = {nums[i] + nums[j]: 1}
            self.records[identifier] = result
            return result
        if j - i == 2:
            result = {
                nums[i] + nums[j]: 1,
                nums[i] + nums[i + 1]: 1,
                nums[j - 1] + nums[j]: 1,
            }
            self.records[identifier] = result
            return result

        if j - i > 2:
            candidates = dict()
            # operation 1:
            val = sum(nums[i:i+2])
            right_outcome = self.aux_marker(nums, i + 2, j)
            if val in right_outcome.keys():
                candidates[val] = right_outcome[val] + 1
            else:
                candidates[val] = 1

            # operation 2:
            val = sum(nums[j-1:j+1])
            left_outcome = self.aux_marker(nums, i, j - 2)
            if val in left_outcome.keys():
                if val not in candidates:
                    candidates[val] = left_outcome[val] + 1
                else: 
                    candidates[val] = max(left_outcome[val] + 1, candidates[val])
            else:
                if val not in candidates:
                    candidates[val] = 1
                else: 
                    candidates[val] = max(1, candidates[val])

            # operation 3:
            val = nums[i] + nums[j]
            middle_outcome = self.aux_marker(nums, i + 1, j - 1)
            if val in middle_outcome.keys():
                if val not in candidates:
                    candidates[val] = middle_outcome[val] + 1
                else: 
                    candidates[val] = max(middle_outcome[val] + 1, candidates[val])
            else:
                if val not in candidates:
                    candidates[val] = 1
                else: 
                    candidates[val] = max(1, candidates[val])

            self.records[identifier] = candidates
            return candidates

333788