返回介绍

solution / 2700-2799 / 2750.Ways to Split Array Into Good Subarrays / README_EN

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

2750. Ways to Split Array Into Good Subarrays

中文文档

Description

You are given a binary array nums.

A subarray of an array is good if it contains exactly one element with the value 1.

Return _an integer denoting the number of ways to split the array _nums_ into good subarrays_. As the number may be too large, return it modulo 109 + 7.

A subarray is a contiguous non-empty sequence of elements within an array.

 

Example 1:

Input: nums = [0,1,0,0,1]
Output: 3
Explanation: There are 3 ways to split nums into good subarrays:
- [0,1] [0,0,1]
- [0,1,0] [0,1]
- [0,1,0,0] [1]

Example 2:

Input: nums = [0,1,0]
Output: 1
Explanation: There is 1 way to split nums into good subarrays:
- [0,1,0]

 

Constraints:

  • 1 <= nums.length <= 105
  • 0 <= nums[i] <= 1

Solutions

Solution 1

class Solution:
  def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:
    mod = 10**9 + 7
    ans, j = 1, -1
    for i, x in enumerate(nums):
      if x == 0:
        continue
      if j > -1:
        ans = ans * (i - j) % mod
      j = i
    return 0 if j == -1 else ans
class Solution {
  public int numberOfGoodSubarraySplits(int[] nums) {
    final int mod = (int) 1e9 + 7;
    int ans = 1, j = -1;
    for (int i = 0; i < nums.length; ++i) {
      if (nums[i] == 0) {
        continue;
      }
      if (j > -1) {
        ans = (int) ((long) ans * (i - j) % mod);
      }
      j = i;
    }
    return j == -1 ? 0 : ans;
  }
}
class Solution {
public:
  int numberOfGoodSubarraySplits(vector<int>& nums) {
    const int mod = 1e9 + 7;
    int ans = 1, j = -1;
    for (int i = 0; i < nums.size(); ++i) {
      if (nums[i] == 0) {
        continue;
      }
      if (j > -1) {
        ans = 1LL * ans * (i - j) % mod;
      }
      j = i;
    }
    return j == -1 ? 0 : ans;
  }
};
func numberOfGoodSubarraySplits(nums []int) int {
  const mod int = 1e9 + 7
  ans, j := 1, -1
  for i, x := range nums {
    if x == 0 {
      continue
    }
    if j > -1 {
      ans = ans * (i - j) % mod
    }
    j = i
  }
  if j == -1 {
    return 0
  }
  return ans
}
function numberOfGoodSubarraySplits(nums: number[]): number {
  let ans = 1;
  let j = -1;
  const mod = 10 ** 9 + 7;
  const n = nums.length;
  for (let i = 0; i < n; ++i) {
    if (nums[i] === 0) {
      continue;
    }
    if (j > -1) {
      ans = (ans * (i - j)) % mod;
    }
    j = i;
  }
  return j === -1 ? 0 : ans;
}
public class Solution {
  public int NumberOfGoodSubarraySplits(int[] nums) {
    long ans = 1, j = -1;
    int mod = 1000000007;
    int n = nums.Length;
    for (int i = 0; i < n; ++i) {
      if (nums[i] == 0) {
        continue;
      }
      if (j > -1) {
        ans = ans * (i - j) % mod;
      }
      j = i;
    }
    return j == -1 ? 0 : (int) ans;
  }
}

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

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

发布评论

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