DFS + Memoization | Easy to understand
We explore the world where the score is:\n- 2 first numbers (nums[0] + nums[1] at first, then nums[l] + nums[l+1])\n- 2 last numbers (nums[nums.size() - 1] + nu
We explore the world where the score is:\n- 2 first numbers (`nums[0] + nums[1]` at first, then `nums[l] + nums[l+1]`)\n- 2 last numbers (`nums[nums.size() - 1] + nums[nums.size() - 2]` at first, then `nums[r] + nums[r-1]`)\n- 1st and the last number (`nums[0] + nums[nums.size() - 1]` at first, then `nums[l] + nums[r]`)\n\nPositions of `l` and `r` tell us where to take our numbers from. `l >= r` is the condition which makes us unable to pick 2 different numbers, so we can return 0.\n\n```cpp\nclass Solution {\npublic:\n  int dfs(int l, int r, int aim, vector<int>& nums, unordered_map<int, unordered_map<int, int>>& memo) {\n    if(l >= r) return 0;\n    if(memo[l][r]) return memo[l][r];\n\n    int maxRes = 0;\n\n    if(aim == nums[l] + nums[l+1]) {\n      maxRes = max(maxRes, dfs(l+2, r, aim, nums, memo) + 1);\n    }\n\n    if(aim == nums[r] + nums[r-1]) {\n      maxRes = max(maxRes, dfs(l, r-2, aim, nums, memo) + 1);\n    }\n\n    if(aim == nums[l] + nums[r]) {\n      maxRes = max(maxRes, dfs(l+1, r-1, aim, nums, memo) + 1);\n    }\n\n    memo[l][r] = maxRes;\n\n    return maxRes;\n  }\n\n  int maxOperations(vector<int>& nums) {\n    int maxOp = 0;\n    unordered_map<int, unordered_map<int, int>> memo;\n\n    int score = nums[0] + nums[1];\n    maxOp = max(maxOp, dfs(2, nums.size() - 1, score, nums, memo) + 1);\n\n    score = nums[nums.size() - 1] + nums[nums.size() - 2];\n    maxOp = max(maxOp, dfs(0, nums.size() - 3, score, nums, memo) + 1);\n\n    score = nums[0] + nums[nums.size() - 1];\n    maxOp = max(maxOp, dfs(1, nums.size() - 2, score, nums, memo) + 1);\n\n    return maxOp;\n  }\n};\n```
['Depth-First Search', 'Recursion', 'Memoization', 'C']