[LeetCode] Maximum Number of Operations With the Same Score I

3038. Maximum Number of Operations With the Same Score I

Given an array of integers called nums, you can perform the following operation while nums contains at least 2 elements:

  • Choose the first two elements of nums and delete them.

The score of the operation is the sum of the deleted elements.

Your task is to find the maximum number of operations that can be performed, such that all operations have the same score.

Return the maximum number of operations possible that satisfy the condition mentioned above.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int maxOperations(vector<int>& A) {
int x = A[0] + A[1];
int res = 0;
for(int i = 0; i + 1 < A.size(); i+= 2) {
if(A[i] + A[i+1] != x) break;
res += 1;
}
return res;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2024/02/18/PS/LeetCode/maximum-number-of-operations-with-the-same-score-i/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.