# LeetCode 1712 将数组分成三个子数组的方案数

left 中元素和小于等于 mid 中元素和，mid 中元素和小于等于 right 中元素和。

## 示例 1：

``````输入：nums = [1,1,1]

``````

## 示例 2：

``````输入：nums = [1,2,2,2,5,0]

[1] [2] [2,2,5,0]
[1] [2,2] [2,5,0]
[1,2] [2,2] [5,0]
``````

## 示例 3：

``````输入：nums = [3,2,1]

``````

``````class Solution {
public:
long long pre[100005] = { 0}, ans = 0, mod = 1e9 + 7, len;
vector<long long> v;

int bs(int l, int r, int p) {
while (l <= r) {
int mid = l + r >> 1;
if (pre[len - 1] - pre[mid] >= pre[mid] - pre[p]) l = mid + 1;
else r = mid - 1;
}
return r;
}

int waysToSplit(vector<int> &nums) {
len = nums.size();
for (int i = 0; i < len; i++) {
if (i == 0) pre[i] = nums[i];
else pre[i] = pre[i - 1] + nums[i];
v.push_back(pre[i]);
}
for (int i = 0; i < len - 2; i++) {
int pos1 = lower_bound(v.begin() + i + 1, v.end(), pre[i] + pre[i]) - v.begin();
if (pos1 == v.size()) break;
int pos2 = bs(pos1, len - 2, i);
ans = (ans + (long long) (pos2 - pos1 + 1)) % mod;
}
return ans;
}
};
``````
原文作者：旺 崽
原文地址: https://blog.csdn.net/qq_43765333/article/details/112170113
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。