[LeetCode] Maximize Score After Pair Deletions

3496. Maximize Score After Pair Deletions

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.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int maxScore(vector<int>& nums) {
int n = nums.size(), remain = nums.size() % 2 ? 1 : 2;
int sum = accumulate(begin(nums), end(nums), 0), res = INT_MIN;
for(int i = 0; i <= n - remain; i++) {
int sub = accumulate(begin(nums) + i, begin(nums) + i + remain, 0);
res = max(res, sum - sub);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/11/22/PS/LeetCode/maximize-score-after-pair-deletions/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.