当前位置:网站首页>leetcode:1567. 乘积为正数的最长子数组长度【dp[i]表示以i结尾的最大长度】
leetcode:1567. 乘积为正数的最长子数组长度【dp[i]表示以i结尾的最大长度】
2022-06-26 21:38:00 【白速龙王的回眸】

分析
同样的,我们需要最长负数和最长正数的乘积的最大长度
其中dp[i]表示选了第i个情况下的最大长度
然后我们可以推导positive[i + 1]和negative[i + 1]的关系式子
if nums[i] > 0:
positive[i] = positive[i - 1] + 1
negative[i] = (negative[i - 1] + 1 if negative[i - 1] > 0 else 0)
elif nums[i] < 0:
positive[i] = (negative[i - 1] + 1 if negative[i - 1] > 0 else 0)
negative[i] = positive[i - 1] + 1
else:
positive[i] = negative[i] = 0
若当前是正数:
positive[i]直接是上一个+1即可
negative[i]的话,如果之前并没有negative的话,乘一个整数也不会是negative,所以是0;如果有的话就+1
若当前是负数:
negative[i]的话直接上一个正数+1
positive[i]的话如果之前没有负数,当前也整不成正数,如果有的话,直接+1
若当前是0
两个重置0
ac code
class Solution:
def getMaxLen(self, nums: List[int]) -> int:
length = len(nums)
positive, negative = [0] * length, [0] * length
if nums[0] > 0:
positive[0] = 1
elif nums[0] < 0:
negative[0] = 1
maxLength = positive[0]
for i in range(1, length):
if nums[i] > 0:
positive[i] = positive[i - 1] + 1
negative[i] = (negative[i - 1] + 1 if negative[i - 1] > 0 else 0)
elif nums[i] < 0:
positive[i] = (negative[i - 1] + 1 if negative[i - 1] > 0 else 0)
negative[i] = positive[i - 1] + 1
else:
positive[i] = negative[i] = 0
maxLength = max(maxLength, positive[i])
return maxLength
总结
dp[i]表示以i结尾的最长xxx
这里同时考虑正负两种情况
边栏推荐
- Final part of web crawler: send directional messages to 100000 Netease cloud users
- Yonghui released the data of Lantern Festival: the sales of Tangyuan increased significantly, and several people's livelihood products increased by more than 150%
- 不同的子序列问题I
- 花店橱窗布置【动态规划】
- Kdd2022 𞓜 unified session recommendation system based on knowledge enhancement prompt learning
- 在哪家证券公司开户最方便最安全可靠
- How to install mysql8.0 database under Windows system? (Graphic tutorial)
- Leetcode question brushing: String 01 (inverted string)
- random_ normal_ Initializer uses
- MacOS環境下使用HomeBrew安裝[email protected]
猜你喜欢

Establish a connection with MySQL

Leetcode question brushing: String 01 (inverted string)

Vi/vim editor

VB.net类库,获取屏幕内鼠标下的颜色(进阶——3)

Web crawler 2: crawl the user ID and home page address of Netease cloud music reviews

12个MySQL慢查询的原因分析

ICML2022 | Neurotoxin:联邦学习的持久后门

Leetcode question brushing: String 02 (reverse string II)

基于SSH框架的学生信息管理系统

Yonghui released the data of Lantern Festival: the sales of Tangyuan increased significantly, and several people's livelihood products increased by more than 150%
随机推荐
Matrix calculator design for beginners of linear algebra based on Qt development
MacOS环境下使用HomeBrew安装[email protected]
俞敏洪:新东方并不存在倒下再翻身,摧毁又雄起的逆转
CVPR 2022 | 美团技术团队精选论文解读
Leetcode question brushing: String 01 (inverted string)
About appium trample pit: encountered internal error running command: error: cannot verify the signature of (solved)
中金证券经理给的开户二维码办理股票开户安全吗?我想开个户
SAP Spartacus 默认路由配置的工作原理
12个MySQL慢查询的原因分析
Cause analysis of 12 MySQL slow queries
Netease Yunxin officially joined the smart hospital branch of China Medical Equipment Association to accelerate the construction of smart hospitals across the country
Vi/vim editor
leetcode刷题:字符串06(实现 strStr())
[Bayesian classification 2] naive Bayesian classifier
The postgraduate entrance examination in these areas is crazy! Which area has the largest number of candidates?
【题解】剑指 Offer 15. 二进制中1的个数(C语言)
Background search, how to find the website background
Shiniman household sprint A shares: annual revenue of nearly 1.2 billion red star Macalline and incredibly home are shareholders
剑指 Offer II 098. 路径的数目 / 剑指 Offer II 099. 最小路径之和
AI intelligent matting tool - hair can be seen