LeetCode 3038: Maximum Number of Operations With the Same Score I (Fixed-Pair Sequential Validation)
LeetCode 3038SimulationGreedyToday we solve LeetCode 3038 - Maximum Number of Operations With the Same Score I.
Source: https://leetcode.com/problems/maximum-number-of-operations-with-the-same-score-i/
English
Problem Summary
In each operation, remove the first two numbers from the array. Every operation must have the same score, where score is the sum of the removed pair. Return the maximum number of operations.
Key Insight
The first operation is forced: we must remove nums[0] and nums[1], so the target score is fixed as nums[0] + nums[1]. After that, each valid operation must be the next adjacent pair in order.
Algorithm
- If length < 2, answer is 0.
- Set target = nums[0] + nums[1], start ans = 1.
- Scan pairs (2,3), (4,5), ....
- If pair sum equals target, increment ans.
- Otherwise stop immediately, because operations must happen in sequence from the front.
Complexity Analysis
Let n be array length.
Time: O(n)
Space: O(1)
Common Pitfalls
- Trying to reorder elements (not allowed).
- Continuing after first mismatch (invalid, because operations are strictly front-first).
- Forgetting odd-length tail handling (last unpaired element is ignored).
Reference Implementations (Java / Go / C++ / Python / JavaScript)
class Solution {
public int maxOperations(int[] nums) {
if (nums.length < 2) return 0;
int target = nums[0] + nums[1];
int ans = 1;
for (int i = 2; i + 1 < nums.length; i += 2) {
if (nums[i] + nums[i + 1] == target) {
ans++;
} else {
break;
}
}
return ans;
}
}func maxOperations(nums []int) int {
if len(nums) < 2 {
return 0
}
target := nums[0] + nums[1]
ans := 1
for i := 2; i+1 < len(nums); i += 2 {
if nums[i]+nums[i+1] == target {
ans++
} else {
break
}
}
return ans
}class Solution {
public:
int maxOperations(vector<int>& nums) {
if (nums.size() < 2) return 0;
int target = nums[0] + nums[1];
int ans = 1;
for (int i = 2; i + 1 < (int)nums.size(); i += 2) {
if (nums[i] + nums[i + 1] == target) {
++ans;
} else {
break;
}
}
return ans;
}
};class Solution:
def maxOperations(self, nums: List[int]) -> int:
if len(nums) < 2:
return 0
target = nums[0] + nums[1]
ans = 1
i = 2
while i + 1 < len(nums):
if nums[i] + nums[i + 1] == target:
ans += 1
i += 2
else:
break
return ansvar maxOperations = function(nums) {
if (nums.length < 2) return 0;
const target = nums[0] + nums[1];
let ans = 1;
for (let i = 2; i + 1 < nums.length; i += 2) {
if (nums[i] + nums[i + 1] === target) {
ans++;
} else {
break;
}
}
return ans;
};中文
题目概述
每次操作都必须删除数组最前面的两个元素,操作得分是这两个数之和。要求所有操作得分都相同,返回最多能进行多少次操作。
核心思路
第一步是被题目固定的:必须删 nums[0] 和 nums[1],因此目标得分固定为 nums[0] + nums[1]。之后只能按顺序检查后续相邻对是否也等于这个目标值。
算法步骤
- 若数组长度小于 2,返回 0。
- 令 target = nums[0] + nums[1],并将答案置为 1。
- 依次检查 (2,3)、(4,5)、... 这些配对。
- 若当前配对和等于 target,答案加 1。
- 一旦不相等,立即停止,因为必须从前往后连续执行操作。
复杂度分析
设数组长度为 n。
时间复杂度:O(n)
空间复杂度:O(1)
常见陷阱
- 把题目误解成可以重排元素(不允许)。
- 出现一次不匹配后还继续尝试(不合法)。
- 忽略奇数长度时最后一个元素无法配对。
多语言参考实现(Java / Go / C++ / Python / JavaScript)
class Solution {
public int maxOperations(int[] nums) {
if (nums.length < 2) return 0;
int target = nums[0] + nums[1];
int ans = 1;
for (int i = 2; i + 1 < nums.length; i += 2) {
if (nums[i] + nums[i + 1] == target) {
ans++;
} else {
break;
}
}
return ans;
}
}func maxOperations(nums []int) int {
if len(nums) < 2 {
return 0
}
target := nums[0] + nums[1]
ans := 1
for i := 2; i+1 < len(nums); i += 2 {
if nums[i]+nums[i+1] == target {
ans++
} else {
break
}
}
return ans
}class Solution {
public:
int maxOperations(vector<int>& nums) {
if (nums.size() < 2) return 0;
int target = nums[0] + nums[1];
int ans = 1;
for (int i = 2; i + 1 < (int)nums.size(); i += 2) {
if (nums[i] + nums[i + 1] == target) {
++ans;
} else {
break;
}
}
return ans;
}
};class Solution:
def maxOperations(self, nums: List[int]) -> int:
if len(nums) < 2:
return 0
target = nums[0] + nums[1]
ans = 1
i = 2
while i + 1 < len(nums):
if nums[i] + nums[i + 1] == target:
ans += 1
i += 2
else:
break
return ansvar maxOperations = function(nums) {
if (nums.length < 2) return 0;
const target = nums[0] + nums[1];
let ans = 1;
for (let i = 2; i + 1 < nums.length; i += 2) {
if (nums[i] + nums[i + 1] === target) {
ans++;
} else {
break;
}
}
return ans;
};
Comments