A split of an integer array is good if:
- The array is split into three non-empty contiguous subarrays - named
left
,mid
,right
respectively from left to right. - The sum of the elements in
left
is less than or equal to the sum of the elements inmid
, and the sum of the elements inmid
is less than or equal to the sum of the elements inright
.
Given nums
, an array of non-negative integers, return the number of good ways to split nums
. As the number may be too large, return it modulo 109 + 7
.
Input: nums = [1,1,1] Output: 1 Explanation: The only good way to split nums is [1] [1] [1].
Input: nums = [1,2,2,2,5,0] Output: 3 Explanation: There are three good ways of splitting nums: [1] [2] [2,2,5,0] [1] [2,2] [2,5,0] [1,2] [2,2] [5,0]
Input: nums = [3,2,1] Output: 0 Explanation: There is no good way to split nums.
3 <= nums.length <= 105
0 <= nums[i] <= 104
impl Solution {
pub fn ways_to_split(mut nums: Vec<i32>) -> i32 {
let mut ret = 0;
for i in 1..nums.len() {
nums[i] += nums[i - 1];
}
let sum = *nums.last().unwrap();
for i in 0..nums.len() - 2 {
let j = match nums[i + 1..].binary_search(&(2 * nums[i] - 1)) {
Ok(a) => a + 1,
Err(b) => b,
};
let k = match nums[i + 1..].binary_search(&((sum - nums[i]) / 2 + nums[i])) {
Ok(a) if a == nums.len() - i - 2 => a,
Ok(a) => a + 1,
Err(b) if b == nums.len() - i - 1 => b - 1,
Err(b) => b,
};
ret = (ret + k.saturating_sub(j) as i32) % 1_000_000_007;
}
ret
}
}