LeetCode 题解工作台
将数组分成三个子数组的方案数
我们称一个分割整数数组的方案是 好的 ,当它满足: 数组被分成三个 非空 连续子数组,从左至右分别命名为 left , mid , right 。 left 中元素和小于等于 mid 中元素和, mid 中元素和小于等于 right 中元素和。 给你一个 非负 整数数组 nums ,请你返回 好的 …
4
题型
5
代码语言
3
相关题
当前训练重点
中等 · 二分·搜索·答案·空间
答案摘要
我们先预处理出数组 的前缀和数组 ,其中 表述数组 前 个元素之和。 由于数组 的元素都是非负整数,因此前缀和数组 是一个单调递增数组。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 二分·搜索·答案·空间 题型思路
题目描述
我们称一个分割整数数组的方案是 好的 ,当它满足:
- 数组被分成三个 非空 连续子数组,从左至右分别命名为
left,mid,right。 left中元素和小于等于mid中元素和,mid中元素和小于等于right中元素和。
给你一个 非负 整数数组 nums ,请你返回 好的 分割 nums 方案数目。由于答案可能会很大,请你将结果对 109 + 7 取余后返回。
示例 1:
输入:nums = [1,1,1] 输出:1 解释:唯一一种好的分割方案是将 nums 分成 [1] [1] [1] 。
示例 2:
输入:nums = [1,2,2,2,5,0] 输出:3 解释:nums 总共有 3 种好的分割方案: [1] [2] [2,2,5,0] [1] [2,2] [2,5,0] [1,2] [2,2] [5,0]
示例 3:
输入:nums = [3,2,1] 输出:0 解释:没有好的分割方案。
提示:
3 <= nums.length <= 1050 <= nums[i] <= 104
解题思路
方法一:前缀和 + 二分查找
我们先预处理出数组 的前缀和数组 ,其中 表述数组 前 个元素之和。
由于数组 的元素都是非负整数,因此前缀和数组 是一个单调递增数组。
我们在 的范围内枚举 left 子数组所能到达的下标 ,然后利用前缀和数组单调递增的特性,通过二分查找的方式找到 mid 子数组分割的合理范围,记为 ,累加方案数 。
二分细节上,子数组分割必须满足 ,并且 。即 ,且 。
最后,将方案数对 取模后返回即可。
时间复杂度 。其中 为数组 的长度。
class Solution:
def waysToSplit(self, nums: List[int]) -> int:
mod = 10**9 + 7
s = list(accumulate(nums))
ans, n = 0, len(nums)
for i in range(n - 2):
j = bisect_left(s, s[i] << 1, i + 1, n - 1)
k = bisect_right(s, (s[-1] + s[i]) >> 1, j, n - 1)
ans += k - j
return ans % mod
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
The candidate demonstrates the ability to handle large inputs efficiently using binary search and prefix sums.
- question_mark
The candidate identifies and uses a two-pointer technique to validate subarray splits in an optimized way.
- question_mark
The candidate understands the trade-off between different approaches and chooses the most efficient one.
常见陷阱
外企场景- error
Not utilizing prefix sums, leading to inefficient subarray sum checks.
- error
Failing to optimize the binary search step, resulting in suboptimal performance for larger inputs.
- error
Overlooking edge cases where no valid split exists, returning incorrect results for such inputs.
进阶变体
外企场景- arrow_right_alt
Different variations of the problem could involve finding splits based on conditions other than sum equality, such as minimum or maximum values.
- arrow_right_alt
This problem could be extended to more than three subarrays, which would introduce more complexity into the binary search and validation steps.
- arrow_right_alt
In some cases, the constraints might change, requiring a solution that works for even larger arrays or different input value ranges.