From 95934b7916a3962ba1d329fd2cf78ae32e0eaa39 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 26 Jun 2023 20:37:06 +0800 Subject: [PATCH 1/2] feat: add solutions to lc problem: No.2750 No.2750.Ways to Split Array Into Good Subarrays --- .../README.md | 90 ++++++++++++++++++- .../README_EN.md | 84 ++++++++++++++++- .../Solution.cpp | 17 ++++ .../Solution.go | 17 ++++ .../Solution.java | 16 ++++ .../Solution.py | 11 +++ .../Solution.ts | 16 ++++ 7 files changed, 245 insertions(+), 6 deletions(-) create mode 100644 solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/Solution.cpp create mode 100644 solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/Solution.go create mode 100644 solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/Solution.java create mode 100644 solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/Solution.py create mode 100644 solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/Solution.ts diff --git a/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/README.md b/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/README.md index 86a2f99fd7a39..9c2d8daaa1cee 100644 --- a/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/README.md +++ b/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/README.md @@ -47,6 +47,12 @@ +**方法一:乘法原理** + +根据题目描述,我们可以在两个 $1$ 之间画一条分割线,假设两个 $1$ 之间的下标分别为 $j$ 和 $i$,那么可以画的不同分割线的数量为 $i - j$。我们找出所有满足条件的 $j$ 和 $i$,然后将所有的 $i - j$ 相乘即可。如果找不到两个 $1$ 之间的分割线,那么说明数组中不存在 $1$,此时答案为 $0$。 + +时间复杂度 $O(n)$,其中 $n$ 为数组长度。空间复杂度 $O(1)$。 + ### **Python3** @@ -54,7 +60,17 @@ ```python - +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 ``` ### **Java** @@ -62,19 +78,87 @@ ```java - +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; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int numberOfGoodSubarraySplits(vector& 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; + } +}; ``` ### **Go** ```go +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 +} +``` +### **TypeScript** + +```ts +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; +} ``` ### **...** diff --git a/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/README_EN.md b/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/README_EN.md index d54742ceabc1d..052b4a81ceb9c 100644 --- a/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/README_EN.md +++ b/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/README_EN.md @@ -48,25 +48,103 @@ ### **Python3** ```python - +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 ``` ### **Java** ```java - +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; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int numberOfGoodSubarraySplits(vector& 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; + } +}; ``` ### **Go** ```go +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 +} +``` +### **TypeScript** + +```ts +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; +} ``` ### **...** diff --git a/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/Solution.cpp b/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/Solution.cpp new file mode 100644 index 0000000000000..274709a64448b --- /dev/null +++ b/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/Solution.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int numberOfGoodSubarraySplits(vector& 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; + } +}; \ No newline at end of file diff --git a/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/Solution.go b/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/Solution.go new file mode 100644 index 0000000000000..41d806d1ee4c2 --- /dev/null +++ b/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/Solution.go @@ -0,0 +1,17 @@ +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 +} \ No newline at end of file diff --git a/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/Solution.java b/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/Solution.java new file mode 100644 index 0000000000000..eec9cc447cd2e --- /dev/null +++ b/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/Solution.java @@ -0,0 +1,16 @@ +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; + } +} \ No newline at end of file diff --git a/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/Solution.py b/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/Solution.py new file mode 100644 index 0000000000000..6b82c14889e40 --- /dev/null +++ b/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/Solution.py @@ -0,0 +1,11 @@ +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 diff --git a/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/Solution.ts b/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/Solution.ts new file mode 100644 index 0000000000000..cb733cbff1857 --- /dev/null +++ b/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/Solution.ts @@ -0,0 +1,16 @@ +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; +} From b009ae85260cef7db367233e66df9f806599697c Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 26 Jun 2023 20:44:05 +0800 Subject: [PATCH 2/2] fix: code --- .../README.md | 22 +++++++++++++++++++ .../README_EN.md | 22 +++++++++++++++++++ .../Solution.cs | 17 ++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/Solution.cs diff --git a/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/README.md b/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/README.md index 9c2d8daaa1cee..43360ff222cfc 100644 --- a/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/README.md +++ b/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/README.md @@ -161,6 +161,28 @@ function numberOfGoodSubarraySplits(nums: number[]): number { } ``` +### **C#** + +```cs +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; + } +} +``` + ### **...** ``` diff --git a/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/README_EN.md b/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/README_EN.md index 052b4a81ceb9c..59f62c2d8ce8c 100644 --- a/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/README_EN.md +++ b/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/README_EN.md @@ -147,6 +147,28 @@ function numberOfGoodSubarraySplits(nums: number[]): number { } ``` +### **C#** + +```cs +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; + } +} +``` + ### **...** ``` diff --git a/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/Solution.cs b/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/Solution.cs new file mode 100644 index 0000000000000..b616a24292772 --- /dev/null +++ b/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/Solution.cs @@ -0,0 +1,17 @@ +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; + } +} \ No newline at end of file