返回介绍

solution / 1700-1799 / 1712.Ways to Split Array Into Three Subarrays / README_EN

发布于 2024-06-17 01:03:15 字数 6271 浏览 0 评论 0 收藏 0

1712. Ways to Split Array Into Three Subarrays

中文文档

Description

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 in mid, and the sum of the elements in mid is less than or equal to the sum of the elements in right.

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.

 

Example 1:

Input: nums = [1,1,1]
Output: 1
Explanation: The only good way to split nums is [1] [1] [1].

Example 2:

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]

Example 3:

Input: nums = [3,2,1]
Output: 0
Explanation: There is no good way to split nums.

 

Constraints:

  • 3 <= nums.length <= 105
  • 0 <= nums[i] <= 104

Solutions

Solution 1: Prefix Sum + Binary Search

First, we preprocess the prefix sum array $s$ of the array $nums$, where $s[i]$ represents the sum of the first $i+1$ elements of the array $nums$.

Since all elements of the array $nums$ are non-negative integers, the prefix sum array $s$ is a monotonically increasing array.

We enumerate the index $i$ that the left subarray can reach in the range $[0,..n-2)$, and then use the monotonically increasing characteristic of the prefix sum array to find the reasonable range of the mid subarray split by binary search, denoted as $[j, k)$, and accumulate the number of schemes $k-j$.

In the binary search details, the subarray split must satisfy $s[j] \geq s[i]$ and $s[n - 1] - s[k] \geq s[k] - s[i]$. That is, $s[j] \geq s[i]$ and $s[k] \leq \frac{s[n - 1] + s[i]}{2}$.

Finally, return the number of schemes modulo $10^9+7$.

The time complexity is $O(n \times \log n)$, where $n$ is the length of the array $nums$.

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
class Solution {
  private static final int MOD = (int) 1e9 + 7;

  public int waysToSplit(int[] nums) {
    int n = nums.length;
    int[] s = new int[n];
    s[0] = nums[0];
    for (int i = 1; i < n; ++i) {
      s[i] = s[i - 1] + nums[i];
    }
    int ans = 0;
    for (int i = 0; i < n - 2; ++i) {
      int j = search(s, s[i] << 1, i + 1, n - 1);
      int k = search(s, ((s[n - 1] + s[i]) >> 1) + 1, j, n - 1);
      ans = (ans + k - j) % MOD;
    }
    return ans;
  }

  private int search(int[] s, int x, int left, int right) {
    while (left < right) {
      int mid = (left + right) >> 1;
      if (s[mid] >= x) {
        right = mid;
      } else {
        left = mid + 1;
      }
    }
    return left;
  }
}
class Solution {
public:
  const int mod = 1e9 + 7;

  int waysToSplit(vector<int>& nums) {
    int n = nums.size();
    vector<int> s(n, nums[0]);
    for (int i = 1; i < n; ++i) s[i] = s[i - 1] + nums[i];
    int ans = 0;
    for (int i = 0; i < n - 2; ++i) {
      int j = lower_bound(s.begin() + i + 1, s.begin() + n - 1, s[i] << 1) - s.begin();
      int k = upper_bound(s.begin() + j, s.begin() + n - 1, (s[n - 1] + s[i]) >> 1) - s.begin();
      ans = (ans + k - j) % mod;
    }
    return ans;
  }
};
func waysToSplit(nums []int) (ans int) {
  const mod int = 1e9 + 7
  n := len(nums)
  s := make([]int, n)
  s[0] = nums[0]
  for i := 1; i < n; i++ {
    s[i] = s[i-1] + nums[i]
  }
  for i := 0; i < n-2; i++ {
    j := sort.Search(n-1, func(h int) bool { return h > i && s[h] >= (s[i]<<1) })
    k := sort.Search(n-1, func(h int) bool { return h >= j && s[h] > (s[n-1]+s[i])>>1 })
    ans = (ans + k - j) % mod
  }
  return
}
/**
 * @param {number[]} nums
 * @return {number}
 */
var waysToSplit = function (nums) {
  const mod = 1e9 + 7;
  const n = nums.length;
  const s = new Array(n).fill(nums[0]);
  for (let i = 1; i < n; ++i) {
    s[i] = s[i - 1] + nums[i];
  }
  function search(s, x, left, right) {
    while (left < right) {
      const mid = (left + right) >> 1;
      if (s[mid] >= x) {
        right = mid;
      } else {
        left = mid + 1;
      }
    }
    return left;
  }
  let ans = 0;
  for (let i = 0; i < n - 2; ++i) {
    const j = search(s, s[i] << 1, i + 1, n - 1);
    const k = search(s, ((s[n - 1] + s[i]) >> 1) + 1, j, n - 1);
    ans = (ans + k - j) % mod;
  }
  return ans;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文