LeetCode 题解工作台
生成平衡数组的方案数
给你一个整数数组 nums 。你需要选择 恰好 一个下标(下标从 0 开始)并删除对应的元素。请注意剩下元素的下标可能会因为删除操作而发生改变。 比方说,如果 nums = [6,1,7,4,1] ,那么: 选择删除下标 1 ,剩下的数组为 nums = [6,7,4,1] 。 选择删除下标 2 ,…
2
题型
5
代码语言
3
相关题
当前训练重点
中等 · 前缀和
答案摘要
我们先预处理得到数组 `nums` 的偶数下标元素之和 以及奇数下标元素之和 。 然后从前往后枚举数组 `nums` 的每个元素 ,用变量 和 分别记录已遍历的偶数下标元素之和以及奇数下标元素之和。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 前缀和 题型思路
题目描述
给你一个整数数组 nums 。你需要选择 恰好 一个下标(下标从 0 开始)并删除对应的元素。请注意剩下元素的下标可能会因为删除操作而发生改变。
比方说,如果 nums = [6,1,7,4,1] ,那么:
- 选择删除下标
1,剩下的数组为nums = [6,7,4,1]。 - 选择删除下标
2,剩下的数组为nums = [6,1,4,1]。 - 选择删除下标
4,剩下的数组为nums = [6,1,7,4]。
如果一个数组满足奇数下标元素的和与偶数下标元素的和相等,该数组就是一个 平衡数组 。
请你返回删除操作后,剩下的数组 nums 是 平衡数组 的 方案数 。
示例 1:
输入:nums = [2,1,6,4] 输出:1 解释: 删除下标 0 :[1,6,4] -> 偶数元素下标为:1 + 4 = 5 。奇数元素下标为:6 。不平衡。 删除下标 1 :[2,6,4] -> 偶数元素下标为:2 + 4 = 6 。奇数元素下标为:6 。平衡。 删除下标 2 :[2,1,4] -> 偶数元素下标为:2 + 4 = 6 。奇数元素下标为:1 。不平衡。 删除下标 3 :[2,1,6] -> 偶数元素下标为:2 + 6 = 8 。奇数元素下标为:1 。不平衡。 只有一种让剩余数组成为平衡数组的方案。
示例 2:
输入:nums = [1,1,1] 输出:3 解释:你可以删除任意元素,剩余数组都是平衡数组。
示例 3:
输入:nums = [1,2,3] 输出:0 解释:不管删除哪个元素,剩下数组都不是平衡数组。
提示:
1 <= nums.length <= 1051 <= nums[i] <= 104
解题思路
方法一:枚举 + 前缀和
我们先预处理得到数组 nums 的偶数下标元素之和 以及奇数下标元素之和 。
然后从前往后枚举数组 nums 的每个元素 ,用变量 和 分别记录已遍历的偶数下标元素之和以及奇数下标元素之和。
我们观察发现,对于当前遍历到的元素 ,如果删除了,那么该元素之后的奇偶下标元素之和会发生交换。此时,我们先判断该位置下标 是奇数还是偶数。
-
如果是偶数下标,删除该元素后,数组的奇数下标元素之和为 ,而偶数下标元素之和为 ,如果这两个和相等,那么就是一个平衡数组,答案加一。
-
如果是奇数下标,删除该元素后,数组的偶数下标元素之和为 ,而奇数下标元素之和为 ,如果这两个和相等,那么就是一个平衡数组,答案加一。
然后我们更新 和 ,继续遍历下一个元素。遍历完数组后,即可得到答案。
时间复杂度 ,其中 为数组的长度。空间复杂度 。
class Solution:
def waysToMakeFair(self, nums: List[int]) -> int:
s1, s2 = sum(nums[::2]), sum(nums[1::2])
ans = t1 = t2 = 0
for i, v in enumerate(nums):
ans += i % 2 == 0 and t2 + s1 - t1 - v == t1 + s2 - t2
ans += i % 2 == 1 and t2 + s1 - t1 == t1 + s2 - t2 - v
t1 += v if i % 2 == 0 else 0
t2 += v if i % 2 == 1 else 0
return ans
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
They mention that indices after the removed element shift, which is a direct hint that right-side parity flips.
- question_mark
They ask for something faster than rebuilding the array for every index, pushing you away from O(n^2) simulation.
- question_mark
They emphasize Array and Prefix Sum topics, signaling parity-based running totals instead of deletion mechanics.
常见陷阱
外企场景- error
Forgetting that only the suffix after the removed index changes parity; the prefix keeps the same even or odd positions.
- error
Checking original even and odd sums after subtracting nums[i] without swapping the right-side parity contribution.
- error
Updating left-side sums before testing the current removal, which makes nums[i] incorrectly remain in the kept array.
进阶变体
外企场景- arrow_right_alt
Return the list of removable indices instead of the count; the same parity-sum check still works.
- arrow_right_alt
Allow multiple fairness queries after point updates to nums; this pushes the problem toward segment trees or Fenwick trees with parity-aware logic.
- arrow_right_alt
Ask whether removing one element can make even and odd sums differ by a target value k instead of zero; the parity-flip formula changes only in the final comparison.