https://leetcode-cn.com/problems/ways-to-split-array-into-three-subarrays/
划分为三个部分。保证left<=mid<=right。
第一刀的切割位置是0到sum/3之间
第二刀的最小值是:第一刀切出来得出的left*2
第二刀的最大值是:第一刀且完之后,剩余部分的均分
求出第一刀每种情况下的,第二刀的最小值和最大值。
class Solution {
public int waysToSplit(int[] nums) {
int n = nums.length;
int[] sums = new int[n];
sums[0] = nums[0];
for(int i=1;i<n;i++){
sums[i] = sums[i-1] + nums[i];
}
final int MOD = (int)(1e9+7);
long ans = 0;
int t = sums[n-1]/3;
for(int i=0;i<n && sums[i]<=t; i++){
int left = lowerBound(i+1,n-1,sums,sums[i]*2);
int right = upperBound(i+1, n-1, sums, sums[i] + (sums[n-1]-sums[i])/2);
if(right >= left){
ans += right-left+1;
}
}
return (int)(ans%MOD);
}
public int lowerBound(int left,int right,int[] nums,int target){
while(left < right){
int mid = left + ((right-left)>>1);
if(nums[mid] < target){
left = mid + 1;
}else{
right = mid;
}
}
return left;
}
public int upperBound(int left,int right,int[] nums,int target){
while(left < right){
int mid = left + ((right-left)>>1);
if(nums[mid] <= target){
left = mid + 1;
}else{
right = mid;
}
}
return left-1;
}
}
注意二分查找的边界问题。