-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.1567
No.1567.Maximum Length of Subarray With Positive Product
- Loading branch information
Showing
12 changed files
with
738 additions
and
293 deletions.
There are no files selected for viewing
340 changes: 253 additions & 87 deletions
340
solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/README.md
Large diffs are not rendered by default.
Oops, something went wrong.
340 changes: 253 additions & 87 deletions
340
...on/1500-1599/1567.Maximum Length of Subarray With Positive Product/README_EN.md
Large diffs are not rendered by default.
Oops, something went wrong.
45 changes: 22 additions & 23 deletions
45
solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,22 @@ | ||
class Solution { | ||
public: | ||
int getMaxLen(vector<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.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; | ||
} | ||
}; | ||
class Solution { | ||
public: | ||
int getMaxLen(vector<int>& nums) { | ||
int n = nums.size(); | ||
vector<int> 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; | ||
} | ||
}; |
64 changes: 32 additions & 32 deletions
64
solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
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 | ||
} |
43 changes: 21 additions & 22 deletions
43
solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
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; | ||
} | ||
} |
42 changes: 17 additions & 25 deletions
42
solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
39 changes: 22 additions & 17 deletions
39
solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
25 changes: 25 additions & 0 deletions
25
solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution2.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class Solution { | ||
public: | ||
int getMaxLen(vector<int>& 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; | ||
} | ||
}; |
29 changes: 29 additions & 0 deletions
29
solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution2.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
24 changes: 24 additions & 0 deletions
24
solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
23 changes: 23 additions & 0 deletions
23
solution/1500-1599/1567.Maximum Length of Subarray With Positive Product/Solution2.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |