当前位置:网站首页>0动态规划 LeetCode1567. 乘积为正数的最长子数组长度
0动态规划 LeetCode1567. 乘积为正数的最长子数组长度
2022-07-26 07:17:00 【18阿鲁】
描述
给你一个整数数组 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
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/maximum-length-of-subarray-with-positive-product
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
分析
分状态一种乘积是负数,一种乘积是正数,两种状态有联系。
dp1[i]表示以nums[i]结尾的乘积是正数的最大子数组长度;
dp2[i]表示以nums[i]结尾的乘积是负数的最大子数组长度;
根据当前元素的正负情况,计算dp1[i],dp2[i]。
class Solution {
public int getMaxLen(int[] nums) {
int[] dp1 = new int[nums.length];
int[] dp2 = new int[nums.length];
dp1[0] = nums[0] > 0 ? 1 : 0;
dp2[0] = nums[0] < 0 ? 1 : 0;
int ans = dp1[0];
for (int i = 1; i < dp1.length; i++) {
if (nums[i] == 0) {
dp1[i] = 0;
dp2[i] = 0;
} else if (nums[i] > 0) {
dp1[i] = dp1[i-1] + 1;
dp2[i] = dp2[i-1] == 0 ? 0 : dp2[i-1] + 1;
} else {
dp1[i] = dp2[i-1] == 0 ? 0 : dp2[i-1] + 1;
dp2[i] = dp1[i-1] + 1;
}
ans = Math.max(ans,dp1[i]);
}
return ans;
}
}
边栏推荐
- Relevant configurations of pychart: change font style and size, change picture background, and change the font color of console output
- Question: can't download sh shellcheck Please install it manually and some commands of shell script
- 【QT】怎样获得QTableView和QTableWidget的行数和列数
- 倒计时2日!基于 Apache DolphinScheduler&TiDB 的交叉开发实践,从编写到调度让你大幅提升效率
- NFT数字藏品开发:数字藏品与NFT的六大区别
- Talent column | can't use Apache dolphin scheduler? The most complete introductory tutorial written by the boss in one month [3]
- 每周小贴士#142:多参数构造函数和explicit
- Weekly tip 142: multi parameter constructors and explicit
- Rgb-t tracking - [dataset benchmark] gtot / rgbt210 / rgbt234 / vot-2019-2020 / laser / VTUAV
- QT: list box, table, tree control
猜你喜欢

Apache Dolphinscheduler3.0.0-beta-1 版本发布,新增FlinkSQL、Zeppelin任务类型

从XSS Playload 学习浏览器解码

QT: list box, table, tree control

Screen: frame paste, 0 fit, full fit

NFT数字藏品开发:数字藏品助力企业发展

20220725 自动控制原理中的补偿器

DaemonSet

To do list application vikunja

redis-migrate-tool迁移报错。

数据平台调度升级改造 | 从Azkaban 平滑过度到 Apache DolphinScheduler 的操作实践
随机推荐
NFT数字藏品系统开发:华为发布首款珍藏版数字藏品
从XSS Playload 学习浏览器解码
College degree sales career, from the third tier 4K to the first tier 20k+, I am very satisfied with myself
达人专栏 | 还不会用 Apache Dolphinscheduler?大佬用时一个月写出的最全入门教程【三】
配置Flask
Weekly tip 142: multi parameter constructors and explicit
正则表达式如何写变量
Drools(2):Drools快速入门
NFT数字藏品开发:数字艺术藏品赋能公益平台
QT: modal, modeless, text box, button, single line input box
20220724 三角函数系的正交性
Apache dolphin scheduler version 3.0.0-beta-1 was released, and flinksql and Zeppelin task types were added
From scratch, we will completely develop an online chess game [Gobang] Based on websocket, and only use dozens of lines of code to complete all the logic.
Opencv learning basic functions
Drools(3):Drools基础语法(1)
How to expand and repartition the C disk?
MySQL安装教程-手把手教你安装
Pycharm的相关配置:改字体样式和大小、更改图片背景、更改控制台输出的字体颜色
Introduction to C language -- summary table of operator priority sorting
NFT数字藏品开发:数字藏品与NFT的六大区别