Maximize Score After Pair Deletions - Problem
You are given an array of integers nums. You must repeatedly perform one of the following operations while the array has more than two elements:
- Remove the first two elements.
- Remove the last two elements.
- Remove the first and last element.
For each operation, add the sum of the removed elements to your total score.
Return the maximum possible score you can achieve.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [6,2,3,4]
›
Output:
15
💡 Note:
Multiple ways to achieve score 15: Remove [6,2] (score 8), then [3,4] (score 7). Total: 8+7=15. Or remove [3,4] first (score 7), then [6,2] (score 8). Both give maximum score 15.
Example 2 — Small Array
$
Input:
nums = [2,7,9,4,5]
›
Output:
27
💡 Note:
Optimal sequence: Remove [2,5] (first+last, score 7), then [7,4] (first+last, score 11), leaving [9] (score 9). Total: 7+11+9=27.
Example 3 — Minimum Size
$
Input:
nums = [1,2]
›
Output:
3
💡 Note:
Array has exactly 2 elements, so we sum them all: 1+2=3. No operations are performed since we need more than 2 elements to operate.
Constraints
- 2 ≤ nums.length ≤ 103
- -106 ≤ nums[i] ≤ 106
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Given array [6,2,3,4] with 3 possible operations
2
Strategy Choice
Each operation removes 2 elements and adds their sum to score
3
Maximum Score
Find the sequence of operations that maximizes total score
Key Takeaway
🎯 Key Insight: Use dynamic programming to explore all possible operation sequences and find the maximum score efficiently
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code