comments | difficulty | edit_url | rating | source | tags | |||
---|---|---|---|---|---|---|---|---|
true |
中等 |
1710 |
第 204 场周赛 Q2 |
|
给你一个整数数组 nums
,请你求出乘积为正数的最长子数组的长度。
一个数组的子数组是由原数组中零个或者更多个连续数字组成的数组。
请你返回乘积为正数的最长子数组长度。
示例 1:
输入:nums = [1,-2,-3,4] 输出:4 解释:数组本身乘积就是正数,值为 24 。
示例 2:
输入:nums = [0,1,-2,-3,-4] 输出:3 解释:最长乘积为正数的子数组为 [1,-2,-3] ,乘积为 6 。 注意,我们不能把 0 也包括到子数组中,因为这样乘积为 0 ,不是正数。
示例 3:
输入:nums = [-1,-2,-3,0,1] 输出:2 解释:乘积为正数的最长子数组是 [-1,-2] 或者 [-2,-3] 。
提示:
1 <= nums.length <= 10^5
-10^9 <= nums[i] <= 10^9
我们定义两个长度为
初始时,如果
接下来,我们从
- 如果
$\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])$ 。
遍历结束后,返回答案
时间复杂度
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
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;
}
}
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;
}
};
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
}
function getMaxLen(nums: number[]): number {
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;
}
我们发现,对于每个
时间复杂度
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
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;
}
}
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;
}
};
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
}
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;
}