当前位置:网站首页>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
这里同时考虑正负两种情况
边栏推荐
- Establish a connection with MySQL
- 十大券商注册开户有没有什么风险?安全吗?
- DAST black box vulnerability scanner part 5: vulnerability scanning engine and service capability
- fastadmin极光推送发送消息的时候registration_id多个用逗号分割后无效
- Android IO, a first-line Internet manufacturer, is a collection of real questions for senior Android interviews
- QT环境下配置Assimp库(MinGW编译器)
- Homebrew installation in MacOS environment [email protected]
- YuMinHong: New Oriental does not have a reversal of falling and turning over, destroying and rising again
- Sword finger offer 12 Path in matrix
- How SAP Spartacus default routing configuration works
猜你喜欢

Leetcode question brushing: String 02 (reverse string II)

回首望月

About appium trample pit: encountered internal error running command: error: cannot verify the signature of (solved)
![[Bayesian classification 3] semi naive Bayesian classifier](/img/9c/070638c1a613be648466e4f2bc341e.png)
[Bayesian classification 3] semi naive Bayesian classifier

Two methods of QT to realize timer

Redis + Guava 本地缓存 API 组合,性能炸裂!

leetcode刷题:哈希表08 (四数之和)

y48.第三章 Kubernetes从入门到精通 -- Pod的状态和探针(二一)

Many gravel 3D material mapping materials can be obtained with one click

12个MySQL慢查询的原因分析
随机推荐
What are the accounting elements
QT环境下配置Assimp库(MinGW编译器)
SAP commerce cloud project Spartacus getting started
Implementation of collaborative filtering evolution version neuralcf and tensorflow2
2022年,中轻度游戏出海路在何方?
The postgraduate entrance examination in these areas is crazy! Which area has the largest number of candidates?
关于appium踩坑 :Encountered internal error running command: Error: Cannot verify the signature of (已解决)
线性模型LN、单神经网络SNN、深度神经网络DNN与CNN测试对比
Introduction of classic wide & deep model and implementation of tensorflow 2 code
网络爬虫2:抓取网易云音乐评论用户ID及主页地址
Sword finger offer 12 Path in matrix
Redis + Guava 本地缓存 API 组合,性能炸裂!
Is there any risk in opening a securities registration account? Is it safe?
Y48. Chapter III kubernetes from introduction to mastery -- pod status and probe (21)
[protobuf] some pits brought by protobuf upgrade
360 mobile assistant is the first to access the app signature service system to help distribute privacy and security
How to enable Hana cloud service on SAP BTP platform
12个MySQL慢查询的原因分析
DAST black box vulnerability scanner part 5: vulnerability scanning engine and service capability
中金证券经理给的开户二维码办理股票开户安全吗?我想开个户