Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.1567
Browse files Browse the repository at this point in the history
No.1567.Maximum Length of Subarray With Positive Product
  • Loading branch information
yanglbme committed Dec 25, 2024
1 parent b466c71 commit d864616
Show file tree
Hide file tree
Showing 12 changed files with 738 additions and 293 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

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;
}
};
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
}
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;
}
}
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
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;
}
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;
}
};
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
}
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;
}
}
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
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;
}

0 comments on commit d864616

Please sign in to comment.