diff --git a/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/README.md b/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/README.md index b0851027fa1ce..f55a8e956a33f 100644 --- a/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/README.md +++ b/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/README.md @@ -69,7 +69,21 @@ tags: -### 方法一 +### 方法一:动态规划 + +我们定义两个长度为 $n$ 的数组 $f$ 和 $g$,其中 $f[i]$ 表示以 $\textit{nums}[i]$ 结尾的乘积为正数的最长子数组的长度,而 $g[i]$ 表示以 $\textit{nums}[i]$ 结尾的乘积为负数的最长子数组的长度。 + +初始时,如果 $\textit{nums}[0] > 0$,则 $f[0] = 1$,否则 $f[0] = 0$;如果 $\textit{nums}[0] < 0$,则 $g[0] = 1$,否则 $g[0] = 0$。我们初始化答案 $ans = f[0]$。 + +接下来,我们从 $i = 1$ 开始遍历数组 $\textit{nums}$,对于每个 $i$,我们有以下几种情况: + +- 如果 $\textit{nums}[i] > 0$,那么 $f[i]$ 可以由 $f[i - 1]$ 转移而来,即 $f[i] = f[i - 1] + 1$,而 $g[i]$ 的值取决于 $g[i - 1]$ 是否为 $0$,如果 $g[i - 1] = 0$,则 $g[i] = 0$,否则 $g[i] = g[i - 1] + 1$; +- 如果 $\textit{nums}[i] < 0$,那么 $f[i]$ 的值取决于 $g[i - 1]$ 是否为 $0$,如果 $g[i - 1] = 0$,则 $f[i] = 0$,否则 $f[i] = g[i - 1] + 1$,而 $g[i]$ 可以由 $f[i - 1]$ 转移而来,即 $g[i] = f[i - 1] + 1$。 +- 然后,我们更新答案 $ans = \max(ans, f[i])$。 + +遍历结束后,返回答案 $ans$ 即可。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 @@ -78,29 +92,21 @@ tags: ```python class Solution: def getMaxLen(self, nums: List[int]) -> int: - f1 = 1 if nums[0] > 0 else 0 - f2 = 1 if nums[0] < 0 else 0 - res = f1 - for num in nums[1:]: - pf1, pf2 = f1, f2 - if num > 0: - f1 += 1 - if f2 > 0: - f2 += 1 - else: - f2 = 0 - elif num < 0: - pf1, pf2 = f1, f2 - f2 = pf1 + 1 - if pf2 > 0: - f1 = pf2 + 1 - else: - f1 = 0 - else: - f1 = 0 - f2 = 0 - res = max(res, f1) - return res + n = len(nums) + f = [0] * n + g = [0] * n + f[0] = int(nums[0] > 0) + g[0] = int(nums[0] < 0) + ans = f[0] + for i in range(1, n): + if nums[i] > 0: + f[i] = f[i - 1] + 1 + g[i] = 0 if g[i - 1] == 0 else g[i - 1] + 1 + elif nums[i] < 0: + f[i] = 0 if g[i - 1] == 0 else g[i - 1] + 1 + g[i] = f[i - 1] + 1 + ans = max(ans, f[i]) + return ans ``` #### Java @@ -108,24 +114,23 @@ class Solution: ```java class Solution { public int getMaxLen(int[] nums) { - int f1 = nums[0] > 0 ? 1 : 0; - int f2 = nums[0] < 0 ? 1 : 0; - int res = f1; - for (int i = 1; i < nums.length; ++i) { + int n = nums.length; + int[] f = new int[n]; + int[] g = new int[n]; + f[0] = nums[0] > 0 ? 1 : 0; + g[0] = nums[0] < 0 ? 1 : 0; + int ans = f[0]; + for (int i = 1; i < n; ++i) { if (nums[i] > 0) { - ++f1; - f2 = f2 > 0 ? f2 + 1 : 0; + f[i] = f[i - 1] + 1; + g[i] = g[i - 1] > 0 ? g[i - 1] + 1 : 0; } else if (nums[i] < 0) { - int pf1 = f1, pf2 = f2; - f2 = pf1 + 1; - f1 = pf2 > 0 ? pf2 + 1 : 0; - } else { - f1 = 0; - f2 = 0; + f[i] = g[i - 1] > 0 ? g[i - 1] + 1 : 0; + g[i] = f[i - 1] + 1; } - res = Math.max(res, f1); + ans = Math.max(ans, f[i]); } - return res; + return ans; } } ``` @@ -136,24 +141,23 @@ class Solution { class Solution { public: int getMaxLen(vector& nums) { - int f1 = nums[0] > 0 ? 1 : 0; - int f2 = nums[0] < 0 ? 1 : 0; - int res = f1; - for (int i = 1; i < nums.size(); ++i) { + int n = nums.size(); + vector f(n, 0), g(n, 0); + f[0] = nums[0] > 0 ? 1 : 0; + g[0] = nums[0] < 0 ? 1 : 0; + int ans = f[0]; + + for (int i = 1; i < n; ++i) { if (nums[i] > 0) { - ++f1; - f2 = f2 > 0 ? f2 + 1 : 0; + f[i] = f[i - 1] + 1; + g[i] = g[i - 1] > 0 ? g[i - 1] + 1 : 0; } else if (nums[i] < 0) { - int pf1 = f1, pf2 = f2; - f2 = pf1 + 1; - f1 = pf2 > 0 ? pf2 + 1 : 0; - } else { - f1 = 0; - f2 = 0; + f[i] = g[i - 1] > 0 ? g[i - 1] + 1 : 0; + g[i] = f[i - 1] + 1; } - res = max(res, f1); + ans = max(ans, f[i]); } - return res; + return ans; } }; ``` @@ -162,36 +166,36 @@ public: ```go func getMaxLen(nums []int) int { - f1, f2 := 0, 0 + n := len(nums) + f := make([]int, n) + g := make([]int, n) if nums[0] > 0 { - f1 = 1 + f[0] = 1 } if nums[0] < 0 { - f2 = 1 + g[0] = 1 } - res := f1 - for i := 1; i < len(nums); i++ { + ans := f[0] + + for i := 1; i < n; i++ { if nums[i] > 0 { - f1++ - if f2 > 0 { - f2++ + f[i] = f[i-1] + 1 + if g[i-1] > 0 { + g[i] = g[i-1] + 1 } else { - f2 = 0 + g[i] = 0 } } else if nums[i] < 0 { - pf1, pf2 := f1, f2 - f2 = pf1 + 1 - if pf2 > 0 { - f1 = pf2 + 1 + if g[i-1] > 0 { + f[i] = g[i-1] + 1 } else { - f1 = 0 + f[i] = 0 } - } else { - f1, f2 = 0, 0 + g[i] = f[i-1] + 1 } - res = max(res, f1) + ans = max(ans, f[i]) } - return res + return ans } ``` @@ -199,24 +203,186 @@ func getMaxLen(nums []int) int { ```ts function getMaxLen(nums: number[]): number { - // 连续正数计数n1, 连续负数计数n2 - let n1 = nums[0] > 0 ? 1 : 0, - n2 = nums[0] < 0 ? 1 : 0; - let ans = n1; - for (let i = 1; i < nums.length; ++i) { - let cur = nums[i]; - if (cur == 0) { - (n1 = 0), (n2 = 0); - } else if (cur > 0) { - ++n1; - n2 = n2 > 0 ? n2 + 1 : 0; - } else { - let t1 = n1, - t2 = n2; - n1 = t2 > 0 ? t2 + 1 : 0; - n2 = t1 + 1; + const n = nums.length; + const f: number[] = Array(n).fill(0); + const g: number[] = Array(n).fill(0); + + if (nums[0] > 0) { + f[0] = 1; + } + if (nums[0] < 0) { + g[0] = 1; + } + + let ans = f[0]; + for (let i = 1; i < n; i++) { + if (nums[i] > 0) { + f[i] = f[i - 1] + 1; + g[i] = g[i - 1] > 0 ? g[i - 1] + 1 : 0; + } else if (nums[i] < 0) { + f[i] = g[i - 1] > 0 ? g[i - 1] + 1 : 0; + g[i] = f[i - 1] + 1; + } + + ans = Math.max(ans, f[i]); + } + + return ans; +} +``` + + + + + + + +### 方法二:动态规划(空间优化) + +我们发现,对于每个 $i$,$f[i]$ 和 $g[i]$ 的值只与 $f[i - 1]$ 和 $g[i - 1]$ 有关,因此我们可以使用两个变量 $f$ 和 $g$ 分别记录 $f[i - 1]$ 和 $g[i - 1]$ 的值,从而将空间复杂度优化至 $O(1)$。 + +时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。 + + + +#### Python3 + +```python +class Solution: + def getMaxLen(self, nums: List[int]) -> int: + n = len(nums) + f = int(nums[0] > 0) + g = int(nums[0] < 0) + ans = f + for i in range(1, n): + ff = gg = 0 + if nums[i] > 0: + ff = f + 1 + gg = 0 if g == 0 else g + 1 + elif nums[i] < 0: + ff = 0 if g == 0 else g + 1 + gg = f + 1 + f, g = ff, gg + ans = max(ans, f) + return ans +``` + +#### Java + +```java +class Solution { + public int getMaxLen(int[] nums) { + int n = nums.length; + int f = nums[0] > 0 ? 1 : 0; + int g = nums[0] < 0 ? 1 : 0; + int ans = f; + + for (int i = 1; i < n; i++) { + int ff = 0, gg = 0; + if (nums[i] > 0) { + ff = f + 1; + gg = g == 0 ? 0 : g + 1; + } else if (nums[i] < 0) { + ff = g == 0 ? 0 : g + 1; + gg = f + 1; + } + f = ff; + g = gg; + ans = Math.max(ans, f); + } + + return ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int getMaxLen(vector& nums) { + int n = nums.size(); + int f = nums[0] > 0 ? 1 : 0; + int g = nums[0] < 0 ? 1 : 0; + int ans = f; + + for (int i = 1; i < n; i++) { + int ff = 0, gg = 0; + if (nums[i] > 0) { + ff = f + 1; + gg = g == 0 ? 0 : g + 1; + } else if (nums[i] < 0) { + ff = g == 0 ? 0 : g + 1; + gg = f + 1; + } + f = ff; + g = gg; + ans = max(ans, f); + } + + return ans; + } +}; +``` + +#### Go + +```go +func getMaxLen(nums []int) int { + n := len(nums) + var f, g int + if nums[0] > 0 { + f = 1 + } else if nums[0] < 0 { + g = 1 + } + ans := f + for i := 1; i < n; i++ { + ff, gg := 0, 0 + if nums[i] > 0 { + ff = f + 1 + gg = 0 + if g > 0 { + gg = g + 1 + } + } else if nums[i] < 0 { + ff = 0 + if g > 0 { + ff = g + 1 + } + gg = f + 1 + } + f, g = ff, gg + ans = max(ans, f) + } + return ans +} +``` + +#### TypeScript + +```ts +function getMaxLen(nums: number[]): number { + const n = nums.length; + let [f, g] = [0, 0]; + if (nums[0] > 0) { + f = 1; + } else if (nums[0] < 0) { + g = 1; + } + let ans = f; + for (let i = 1; i < n; i++) { + let [ff, gg] = [0, 0]; + if (nums[i] > 0) { + ff = f + 1; + gg = g > 0 ? g + 1 : 0; + } else if (nums[i] < 0) { + ff = g > 0 ? g + 1 : 0; + gg = f + 1; } - ans = Math.max(ans, n1); + [f, g] = [ff, gg]; + ans = Math.max(ans, f); } return ans; } diff --git a/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/README_EN.md b/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/README_EN.md index 0a8fa37343e95..fa1e74da31c57 100644 --- a/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/README_EN.md +++ b/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/README_EN.md @@ -65,7 +65,21 @@ Notice that we cannot include 0 in the subarray since that'll make the produ -### Solution 1 +### Solution 1: Dynamic Programming + +We define two arrays $f$ and $g$ of length $n$, where $f[i]$ represents the length of the longest subarray ending at $\textit{nums}[i]$ with a positive product, and $g[i]$ represents the length of the longest subarray ending at $\textit{nums}[i]$ with a negative product. + +Initially, if $\textit{nums}[0] > 0$, then $f[0] = 1$, otherwise $f[0] = 0$; if $\textit{nums}[0] < 0$, then $g[0] = 1$, otherwise $g[0] = 0$. We initialize the answer $ans = f[0]$. + +Next, we iterate through the array $\textit{nums}$ starting from $i = 1$. For each $i$, we have the following cases: + +- If $\textit{nums}[i] > 0$, then $f[i]$ can be transferred from $f[i - 1]$, i.e., $f[i] = f[i - 1] + 1$, and the value of $g[i]$ depends on whether $g[i - 1]$ is $0$. If $g[i - 1] = 0$, then $g[i] = 0$, otherwise $g[i] = g[i - 1] + 1$; +- If $\textit{nums}[i] < 0$, then the value of $f[i]$ depends on whether $g[i - 1]$ is $0$. If $g[i - 1] = 0$, then $f[i] = 0$, otherwise $f[i] = g[i - 1] + 1$, and $g[i]$ can be transferred from $f[i - 1]$, i.e., $g[i] = f[i - 1] + 1$. +- Then, we update the answer $ans = \max(ans, f[i])$. + +After the iteration, we return the answer $ans$. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$. @@ -74,29 +88,21 @@ Notice that we cannot include 0 in the subarray since that'll make the produ ```python class Solution: def getMaxLen(self, nums: List[int]) -> int: - f1 = 1 if nums[0] > 0 else 0 - f2 = 1 if nums[0] < 0 else 0 - res = f1 - for num in nums[1:]: - pf1, pf2 = f1, f2 - if num > 0: - f1 += 1 - if f2 > 0: - f2 += 1 - else: - f2 = 0 - elif num < 0: - pf1, pf2 = f1, f2 - f2 = pf1 + 1 - if pf2 > 0: - f1 = pf2 + 1 - else: - f1 = 0 - else: - f1 = 0 - f2 = 0 - res = max(res, f1) - return res + n = len(nums) + f = [0] * n + g = [0] * n + f[0] = int(nums[0] > 0) + g[0] = int(nums[0] < 0) + ans = f[0] + for i in range(1, n): + if nums[i] > 0: + f[i] = f[i - 1] + 1 + g[i] = 0 if g[i - 1] == 0 else g[i - 1] + 1 + elif nums[i] < 0: + f[i] = 0 if g[i - 1] == 0 else g[i - 1] + 1 + g[i] = f[i - 1] + 1 + ans = max(ans, f[i]) + return ans ``` #### Java @@ -104,24 +110,23 @@ class Solution: ```java class Solution { public int getMaxLen(int[] nums) { - int f1 = nums[0] > 0 ? 1 : 0; - int f2 = nums[0] < 0 ? 1 : 0; - int res = f1; - for (int i = 1; i < nums.length; ++i) { + int n = nums.length; + int[] f = new int[n]; + int[] g = new int[n]; + f[0] = nums[0] > 0 ? 1 : 0; + g[0] = nums[0] < 0 ? 1 : 0; + int ans = f[0]; + for (int i = 1; i < n; ++i) { if (nums[i] > 0) { - ++f1; - f2 = f2 > 0 ? f2 + 1 : 0; + f[i] = f[i - 1] + 1; + g[i] = g[i - 1] > 0 ? g[i - 1] + 1 : 0; } else if (nums[i] < 0) { - int pf1 = f1, pf2 = f2; - f2 = pf1 + 1; - f1 = pf2 > 0 ? pf2 + 1 : 0; - } else { - f1 = 0; - f2 = 0; + f[i] = g[i - 1] > 0 ? g[i - 1] + 1 : 0; + g[i] = f[i - 1] + 1; } - res = Math.max(res, f1); + ans = Math.max(ans, f[i]); } - return res; + return ans; } } ``` @@ -132,24 +137,23 @@ class Solution { class Solution { public: int getMaxLen(vector& nums) { - int f1 = nums[0] > 0 ? 1 : 0; - int f2 = nums[0] < 0 ? 1 : 0; - int res = f1; - for (int i = 1; i < nums.size(); ++i) { + int n = nums.size(); + vector f(n, 0), g(n, 0); + f[0] = nums[0] > 0 ? 1 : 0; + g[0] = nums[0] < 0 ? 1 : 0; + int ans = f[0]; + + for (int i = 1; i < n; ++i) { if (nums[i] > 0) { - ++f1; - f2 = f2 > 0 ? f2 + 1 : 0; + f[i] = f[i - 1] + 1; + g[i] = g[i - 1] > 0 ? g[i - 1] + 1 : 0; } else if (nums[i] < 0) { - int pf1 = f1, pf2 = f2; - f2 = pf1 + 1; - f1 = pf2 > 0 ? pf2 + 1 : 0; - } else { - f1 = 0; - f2 = 0; + f[i] = g[i - 1] > 0 ? g[i - 1] + 1 : 0; + g[i] = f[i - 1] + 1; } - res = max(res, f1); + ans = max(ans, f[i]); } - return res; + return ans; } }; ``` @@ -158,36 +162,36 @@ public: ```go func getMaxLen(nums []int) int { - f1, f2 := 0, 0 + n := len(nums) + f := make([]int, n) + g := make([]int, n) if nums[0] > 0 { - f1 = 1 + f[0] = 1 } if nums[0] < 0 { - f2 = 1 + g[0] = 1 } - res := f1 - for i := 1; i < len(nums); i++ { + ans := f[0] + + for i := 1; i < n; i++ { if nums[i] > 0 { - f1++ - if f2 > 0 { - f2++ + f[i] = f[i-1] + 1 + if g[i-1] > 0 { + g[i] = g[i-1] + 1 } else { - f2 = 0 + g[i] = 0 } } else if nums[i] < 0 { - pf1, pf2 := f1, f2 - f2 = pf1 + 1 - if pf2 > 0 { - f1 = pf2 + 1 + if g[i-1] > 0 { + f[i] = g[i-1] + 1 } else { - f1 = 0 + f[i] = 0 } - } else { - f1, f2 = 0, 0 + g[i] = f[i-1] + 1 } - res = max(res, f1) + ans = max(ans, f[i]) } - return res + return ans } ``` @@ -195,24 +199,186 @@ func getMaxLen(nums []int) int { ```ts function getMaxLen(nums: number[]): number { - // 连续正数计数n1, 连续负数计数n2 - let n1 = nums[0] > 0 ? 1 : 0, - n2 = nums[0] < 0 ? 1 : 0; - let ans = n1; - for (let i = 1; i < nums.length; ++i) { - let cur = nums[i]; - if (cur == 0) { - (n1 = 0), (n2 = 0); - } else if (cur > 0) { - ++n1; - n2 = n2 > 0 ? n2 + 1 : 0; - } else { - let t1 = n1, - t2 = n2; - n1 = t2 > 0 ? t2 + 1 : 0; - n2 = t1 + 1; + const n = nums.length; + const f: number[] = Array(n).fill(0); + const g: number[] = Array(n).fill(0); + + if (nums[0] > 0) { + f[0] = 1; + } + if (nums[0] < 0) { + g[0] = 1; + } + + let ans = f[0]; + for (let i = 1; i < n; i++) { + if (nums[i] > 0) { + f[i] = f[i - 1] + 1; + g[i] = g[i - 1] > 0 ? g[i - 1] + 1 : 0; + } else if (nums[i] < 0) { + f[i] = g[i - 1] > 0 ? g[i - 1] + 1 : 0; + g[i] = f[i - 1] + 1; + } + + ans = Math.max(ans, f[i]); + } + + return ans; +} +``` + + + + + + + +### Solution 2: Dynamic Programming (Space Optimization) + +We observe that for each $i$, the values of $f[i]$ and $g[i]$ only depend on $f[i - 1]$ and $g[i - 1]$. Therefore, we can use two variables $f$ and $g$ to record the values of $f[i - 1]$ and $g[i - 1]$, respectively, thus optimizing the space complexity to $O(1)$. + +The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$. + + + +#### Python3 + +```python +class Solution: + def getMaxLen(self, nums: List[int]) -> int: + n = len(nums) + f = int(nums[0] > 0) + g = int(nums[0] < 0) + ans = f + for i in range(1, n): + ff = gg = 0 + if nums[i] > 0: + ff = f + 1 + gg = 0 if g == 0 else g + 1 + elif nums[i] < 0: + ff = 0 if g == 0 else g + 1 + gg = f + 1 + f, g = ff, gg + ans = max(ans, f) + return ans +``` + +#### Java + +```java +class Solution { + public int getMaxLen(int[] nums) { + int n = nums.length; + int f = nums[0] > 0 ? 1 : 0; + int g = nums[0] < 0 ? 1 : 0; + int ans = f; + + for (int i = 1; i < n; i++) { + int ff = 0, gg = 0; + if (nums[i] > 0) { + ff = f + 1; + gg = g == 0 ? 0 : g + 1; + } else if (nums[i] < 0) { + ff = g == 0 ? 0 : g + 1; + gg = f + 1; + } + f = ff; + g = gg; + ans = Math.max(ans, f); + } + + return ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int getMaxLen(vector& nums) { + int n = nums.size(); + int f = nums[0] > 0 ? 1 : 0; + int g = nums[0] < 0 ? 1 : 0; + int ans = f; + + for (int i = 1; i < n; i++) { + int ff = 0, gg = 0; + if (nums[i] > 0) { + ff = f + 1; + gg = g == 0 ? 0 : g + 1; + } else if (nums[i] < 0) { + ff = g == 0 ? 0 : g + 1; + gg = f + 1; + } + f = ff; + g = gg; + ans = max(ans, f); + } + + return ans; + } +}; +``` + +#### Go + +```go +func getMaxLen(nums []int) int { + n := len(nums) + var f, g int + if nums[0] > 0 { + f = 1 + } else if nums[0] < 0 { + g = 1 + } + ans := f + for i := 1; i < n; i++ { + ff, gg := 0, 0 + if nums[i] > 0 { + ff = f + 1 + gg = 0 + if g > 0 { + gg = g + 1 + } + } else if nums[i] < 0 { + ff = 0 + if g > 0 { + ff = g + 1 + } + gg = f + 1 + } + f, g = ff, gg + ans = max(ans, f) + } + return ans +} +``` + +#### TypeScript + +```ts +function getMaxLen(nums: number[]): number { + const n = nums.length; + let [f, g] = [0, 0]; + if (nums[0] > 0) { + f = 1; + } else if (nums[0] < 0) { + g = 1; + } + let ans = f; + for (let i = 1; i < n; i++) { + let [ff, gg] = [0, 0]; + if (nums[i] > 0) { + ff = f + 1; + gg = g > 0 ? g + 1 : 0; + } else if (nums[i] < 0) { + ff = g > 0 ? g + 1 : 0; + gg = f + 1; } - ans = Math.max(ans, n1); + [f, g] = [ff, gg]; + ans = Math.max(ans, f); } return ans; } diff --git a/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution.cpp b/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution.cpp index e7e20b51bd329..c9ef140cdbdad 100644 --- a/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution.cpp +++ b/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution.cpp @@ -1,23 +1,22 @@ -class Solution { -public: - int getMaxLen(vector& nums) { - int f1 = nums[0] > 0 ? 1 : 0; - int f2 = nums[0] < 0 ? 1 : 0; - int res = f1; - for (int i = 1; i < nums.size(); ++i) { - if (nums[i] > 0) { - ++f1; - f2 = f2 > 0 ? f2 + 1 : 0; - } else if (nums[i] < 0) { - int pf1 = f1, pf2 = f2; - f2 = pf1 + 1; - f1 = pf2 > 0 ? pf2 + 1 : 0; - } else { - f1 = 0; - f2 = 0; - } - res = max(res, f1); - } - return res; - } -}; \ No newline at end of file +class Solution { +public: + int getMaxLen(vector& nums) { + int n = nums.size(); + vector f(n, 0), g(n, 0); + f[0] = nums[0] > 0 ? 1 : 0; + g[0] = nums[0] < 0 ? 1 : 0; + int ans = f[0]; + + for (int i = 1; i < n; ++i) { + if (nums[i] > 0) { + f[i] = f[i - 1] + 1; + g[i] = g[i - 1] > 0 ? g[i - 1] + 1 : 0; + } else if (nums[i] < 0) { + f[i] = g[i - 1] > 0 ? g[i - 1] + 1 : 0; + g[i] = f[i - 1] + 1; + } + ans = max(ans, f[i]); + } + return ans; + } +}; diff --git a/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution.go b/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution.go index 18ab80619fd69..c9e7417136fc8 100644 --- a/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution.go +++ b/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution.go @@ -1,32 +1,32 @@ -func getMaxLen(nums []int) int { - f1, f2 := 0, 0 - if nums[0] > 0 { - f1 = 1 - } - if nums[0] < 0 { - f2 = 1 - } - res := f1 - for i := 1; i < len(nums); i++ { - if nums[i] > 0 { - f1++ - if f2 > 0 { - f2++ - } else { - f2 = 0 - } - } else if nums[i] < 0 { - pf1, pf2 := f1, f2 - f2 = pf1 + 1 - if pf2 > 0 { - f1 = pf2 + 1 - } else { - f1 = 0 - } - } else { - f1, f2 = 0, 0 - } - res = max(res, f1) - } - return res -} \ No newline at end of file +func getMaxLen(nums []int) int { + n := len(nums) + f := make([]int, n) + g := make([]int, n) + if nums[0] > 0 { + f[0] = 1 + } + if nums[0] < 0 { + g[0] = 1 + } + ans := f[0] + + for i := 1; i < n; i++ { + if nums[i] > 0 { + f[i] = f[i-1] + 1 + if g[i-1] > 0 { + g[i] = g[i-1] + 1 + } else { + g[i] = 0 + } + } else if nums[i] < 0 { + if g[i-1] > 0 { + f[i] = g[i-1] + 1 + } else { + f[i] = 0 + } + g[i] = f[i-1] + 1 + } + ans = max(ans, f[i]) + } + return ans +} diff --git a/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution.java b/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution.java index 6f7c66d2f823c..93155987b36c1 100644 --- a/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution.java +++ b/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution.java @@ -1,22 +1,21 @@ -class Solution { - public int getMaxLen(int[] nums) { - int f1 = nums[0] > 0 ? 1 : 0; - int f2 = nums[0] < 0 ? 1 : 0; - int res = f1; - for (int i = 1; i < nums.length; ++i) { - if (nums[i] > 0) { - ++f1; - f2 = f2 > 0 ? f2 + 1 : 0; - } else if (nums[i] < 0) { - int pf1 = f1, pf2 = f2; - f2 = pf1 + 1; - f1 = pf2 > 0 ? pf2 + 1 : 0; - } else { - f1 = 0; - f2 = 0; - } - res = Math.max(res, f1); - } - return res; - } -} \ No newline at end of file +class Solution { + public int getMaxLen(int[] nums) { + int n = nums.length; + int[] f = new int[n]; + int[] g = new int[n]; + f[0] = nums[0] > 0 ? 1 : 0; + g[0] = nums[0] < 0 ? 1 : 0; + int ans = f[0]; + for (int i = 1; i < n; ++i) { + if (nums[i] > 0) { + f[i] = f[i - 1] + 1; + g[i] = g[i - 1] > 0 ? g[i - 1] + 1 : 0; + } else if (nums[i] < 0) { + f[i] = g[i - 1] > 0 ? g[i - 1] + 1 : 0; + g[i] = f[i - 1] + 1; + } + ans = Math.max(ans, f[i]); + } + return ans; + } +} diff --git a/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution.py b/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution.py index 779c7667a54d0..fa8b933798dad 100644 --- a/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution.py +++ b/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution.py @@ -1,25 +1,17 @@ -class Solution: - def getMaxLen(self, nums: List[int]) -> int: - f1 = 1 if nums[0] > 0 else 0 - f2 = 1 if nums[0] < 0 else 0 - res = f1 - for num in nums[1:]: - pf1, pf2 = f1, f2 - if num > 0: - f1 += 1 - if f2 > 0: - f2 += 1 - else: - f2 = 0 - elif num < 0: - pf1, pf2 = f1, f2 - f2 = pf1 + 1 - if pf2 > 0: - f1 = pf2 + 1 - else: - f1 = 0 - else: - f1 = 0 - f2 = 0 - res = max(res, f1) - return res +class Solution: + def getMaxLen(self, nums: List[int]) -> int: + n = len(nums) + f = [0] * n + g = [0] * n + f[0] = int(nums[0] > 0) + g[0] = int(nums[0] < 0) + ans = f[0] + for i in range(1, n): + if nums[i] > 0: + f[i] = f[i - 1] + 1 + g[i] = 0 if g[i - 1] == 0 else g[i - 1] + 1 + elif nums[i] < 0: + f[i] = 0 if g[i - 1] == 0 else g[i - 1] + 1 + g[i] = f[i - 1] + 1 + ans = max(ans, f[i]) + return ans diff --git a/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution.ts b/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution.ts index ac7d9632db5fb..dbc1176c73ed0 100644 --- a/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution.ts +++ b/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution.ts @@ -1,22 +1,27 @@ function getMaxLen(nums: number[]): number { - // 连续正数计数n1, 连续负数计数n2 - let n1 = nums[0] > 0 ? 1 : 0, - n2 = nums[0] < 0 ? 1 : 0; - let ans = n1; - for (let i = 1; i < nums.length; ++i) { - let cur = nums[i]; - if (cur == 0) { - (n1 = 0), (n2 = 0); - } else if (cur > 0) { - ++n1; - n2 = n2 > 0 ? n2 + 1 : 0; - } else { - let t1 = n1, - t2 = n2; - n1 = t2 > 0 ? t2 + 1 : 0; - n2 = t1 + 1; + const n = nums.length; + const f: number[] = Array(n).fill(0); + const g: number[] = Array(n).fill(0); + + if (nums[0] > 0) { + f[0] = 1; + } + if (nums[0] < 0) { + g[0] = 1; + } + + let ans = f[0]; + for (let i = 1; i < n; i++) { + if (nums[i] > 0) { + f[i] = f[i - 1] + 1; + g[i] = g[i - 1] > 0 ? g[i - 1] + 1 : 0; + } else if (nums[i] < 0) { + f[i] = g[i - 1] > 0 ? g[i - 1] + 1 : 0; + g[i] = f[i - 1] + 1; } - ans = Math.max(ans, n1); + + ans = Math.max(ans, f[i]); } + return ans; } diff --git a/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution2.cpp b/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution2.cpp new file mode 100644 index 0000000000000..7564331bf09da --- /dev/null +++ b/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution2.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int getMaxLen(vector& nums) { + int n = nums.size(); + int f = nums[0] > 0 ? 1 : 0; + int g = nums[0] < 0 ? 1 : 0; + int ans = f; + + for (int i = 1; i < n; i++) { + int ff = 0, gg = 0; + if (nums[i] > 0) { + ff = f + 1; + gg = g == 0 ? 0 : g + 1; + } else if (nums[i] < 0) { + ff = g == 0 ? 0 : g + 1; + gg = f + 1; + } + f = ff; + g = gg; + ans = max(ans, f); + } + + return ans; + } +}; diff --git a/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution2.go b/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution2.go new file mode 100644 index 0000000000000..c7e56aaadebe0 --- /dev/null +++ b/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution2.go @@ -0,0 +1,29 @@ +func getMaxLen(nums []int) int { + n := len(nums) + var f, g int + if nums[0] > 0 { + f = 1 + } else if nums[0] < 0 { + g = 1 + } + ans := f + for i := 1; i < n; i++ { + ff, gg := 0, 0 + if nums[i] > 0 { + ff = f + 1 + gg = 0 + if g > 0 { + gg = g + 1 + } + } else if nums[i] < 0 { + ff = 0 + if g > 0 { + ff = g + 1 + } + gg = f + 1 + } + f, g = ff, gg + ans = max(ans, f) + } + return ans +} diff --git a/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution2.java b/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution2.java new file mode 100644 index 0000000000000..b38fce770f3e9 --- /dev/null +++ b/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution2.java @@ -0,0 +1,24 @@ +class Solution { + public int getMaxLen(int[] nums) { + int n = nums.length; + int f = nums[0] > 0 ? 1 : 0; + int g = nums[0] < 0 ? 1 : 0; + int ans = f; + + for (int i = 1; i < n; i++) { + int ff = 0, gg = 0; + if (nums[i] > 0) { + ff = f + 1; + gg = g == 0 ? 0 : g + 1; + } else if (nums[i] < 0) { + ff = g == 0 ? 0 : g + 1; + gg = f + 1; + } + f = ff; + g = gg; + ans = Math.max(ans, f); + } + + return ans; + } +} diff --git a/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution2.py b/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution2.py new file mode 100644 index 0000000000000..8018ca1c0e975 --- /dev/null +++ b/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution2.py @@ -0,0 +1,17 @@ +class Solution: + def getMaxLen(self, nums: List[int]) -> int: + n = len(nums) + f = int(nums[0] > 0) + g = int(nums[0] < 0) + ans = f + for i in range(1, n): + ff = gg = 0 + if nums[i] > 0: + ff = f + 1 + gg = 0 if g == 0 else g + 1 + elif nums[i] < 0: + ff = 0 if g == 0 else g + 1 + gg = f + 1 + f, g = ff, gg + ans = max(ans, f) + return ans diff --git a/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution2.ts b/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution2.ts new file mode 100644 index 0000000000000..787401390b9c0 --- /dev/null +++ b/solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution2.ts @@ -0,0 +1,23 @@ +function getMaxLen(nums: number[]): number { + const n = nums.length; + let [f, g] = [0, 0]; + if (nums[0] > 0) { + f = 1; + } else if (nums[0] < 0) { + g = 1; + } + let ans = f; + for (let i = 1; i < n; i++) { + let [ff, gg] = [0, 0]; + if (nums[i] > 0) { + ff = f + 1; + gg = g > 0 ? g + 1 : 0; + } else if (nums[i] < 0) { + ff = g > 0 ? g + 1 : 0; + gg = f + 1; + } + [f, g] = [ff, gg]; + ans = Math.max(ans, f); + } + return ans; +}