#3040

Maximum Number of Operations With the Same Score II

expert · 1085 · lc medium +32 · verified · 34.1% accepted · 187 likes · top 12%

Description

You are given an integer array nums. While nums has at least 2 elements, you may perform one of:

- Remove the first two elements.

- Remove the last two elements.

- Remove the first and last elements.

Each operation has a score equal to the sum of the removed elements. All operations must have the same score.

Return the maximum number of operations achievable under this constraint.

Example 1:

Input: nums = [3,2,1,2,3,4]
Output: 3
Explanation: 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.

Example 2:

Input: nums = [3,2,6,1,4]
Output: 2
Explanation: 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.

Code

1
2
3