当前位置:网站首页>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
这里同时考虑正负两种情况
边栏推荐
- 聊聊我的远程工作体验 | 社区征文
- AI intelligent matting tool - hair can be seen
- 关于appium踩坑 :Encountered internal error running command: Error: Cannot verify the signature of (已解决)
- Looking back at the moon
- [protobuf] some pits brought by protobuf upgrade
- 中金证券经理给的开户二维码办理股票开户安全吗?我想开个户
- Web crawler 2: crawl the user ID and home page address of Netease cloud music reviews
- 记录一次Redis大Key的排查
- Matrix calculator design for beginners of linear algebra based on Qt development
- Usage of MGrid in numpy
猜你喜欢

大家都能看得懂的源码(一)ahooks 整体架构篇

Godson China Science and technology innovation board is listed: the market value is 35.7 billion yuan, becoming the first share of domestic CPU

Configure redis master-slave and sentinel sentinel in the centos7 environment (solve the problem that the sentinel does not switch when the master hangs up in the ECS)

Leetcode question brushing: String 05 (Sword finger offer 58 - ii. left rotation string)

Y48. Chapter III kubernetes from introduction to mastery -- pod status and probe (21)

亿级月活全民K歌Feed业务在腾讯云MongoDB中的应用及优化实践

「连续学习Continual learning, CL」最新2022研究综述

Shiniman household sprint A shares: annual revenue of nearly 1.2 billion red star Macalline and incredibly home are shareholders

Gamefi active users, transaction volume, financing amount and new projects continue to decline. Can axie and stepn get rid of the death spiral? Where is the chain tour?
![[leetcode]- linked list-2](/img/f7/9d4b01285fd6f7fa9f3431985111b0.png)
[leetcode]- linked list-2
随机推荐
基于Qt实现的“合成大西瓜”小游戏
Dynamic parameter association using postman
leetcode刷题:字符串03(剑指 Offer 05. 替换空格)
Installation avec homebrew dans un environnement Mac OS [email protected]
2022年,中轻度游戏出海路在何方?
[solution] sword finger offer 15 Number of 1 in binary (C language)
DAST black box vulnerability scanner part 5: vulnerability scanning engine and service capability
宝藏又小众的覆盖物PBR多通道贴图素材网站分享
Y48. Chapter III kubernetes from introduction to mastery -- pod status and probe (21)
leetcode刷题:字符串04(颠倒字符串中的单词)
中金证券经理给的开户二维码办理股票开户安全吗?我想开个户
YOLOv6:又快又准的目標檢測框架開源啦
Leetcode question brushing: String 02 (reverse string II)
SAP Spartacus 中的依赖注入 Dependency Injection 介绍
Talk about my remote work experience | community essay solicitation
leetcode刷题:字符串05(剑指 Offer 58 - II. 左旋转字符串)
不要做巨嬰了
Kdd2022 𞓜 unified session recommendation system based on knowledge enhancement prompt learning
Yonghui released the data of Lantern Festival: the sales of Tangyuan increased significantly, and several people's livelihood products increased by more than 150%
基于启发式搜索的一字棋