当前位置:网站首页>leetcode:1567. Length of the longest subarray whose product is a positive number [dp[i] indicates the maximum length ending with I]
leetcode:1567. Length of the longest subarray whose product is a positive number [dp[i] indicates the maximum length ending with I]
2022-06-26 21:43:00 【Review of the white speed Dragon King】

analysis
alike , We need the maximum length of the product of the longest negative number and the longest positive number
among dp[i] It means that the... Is selected i Maximum length in cases
Then we can deduce positive[i + 1] and negative[i + 1] The relationship between
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
If the current number is positive :
positive[i] It's the last one +1 that will do
negative[i] Words , If not before negative Words , Multiplying by an integer won't be negative, So it is 0; If so +1
If the current number is negative :
negative[i] If so, go directly to a positive number +1
positive[i] If there were no negative numbers before , At present, it is not a positive number , If any , direct +1
If it is 0
Two resets 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
summary
dp[i] Said to i Longest ending xxx
Both positive and negative cases are considered here
边栏推荐
- KDD2022 | 基于知识增强提示学习的统一会话推荐系统
- Test comparison of linear model LN, single neural network SNN, deep neural network DNN and CNN
- leetcode刷题:字符串03(剑指 Offer 05. 替换空格)
- Stop being a giant baby
- 【 protobuf 】 quelques puits causés par la mise à niveau de protobuf
- MATLAB and MySQL database connection and data exchange (based on ODBC)
- 诗尼曼家居冲刺A股:年营收近12亿 红星美凯龙与居然之家是股东
- lotus configurations
- 同花顺注册开户有没有什么风险?安全吗?
- Yonghui released the data of Lantern Festival: the sales of Tangyuan increased significantly, and several people's livelihood products increased by more than 150%
猜你喜欢

ICML2022 | Neurotoxin:联邦学习的持久后门
![[solution] sword finger offer 15 Number of 1 in binary (C language)](/img/ab/149775ae8ed94464efdf6921c1022a.png)
[solution] sword finger offer 15 Number of 1 in binary (C language)

Netease Yunxin officially joined the smart hospital branch of China Medical Equipment Association to accelerate the construction of smart hospitals across the country

DAST 黑盒漏洞扫描器 第五篇:漏洞扫描引擎与服务能力

经典Wide & Deep模型介绍及tensorflow 2代码实现

2022年,中轻度游戏出海路在何方?

众多碎石3d材质贴图素材一键即可获取
![[LeetCode]-链表-2](/img/f7/9d4b01285fd6f7fa9f3431985111b0.png)
[LeetCode]-链表-2

leetcode刷题:字符串03(剑指 Offer 05. 替换空格)

茂莱光学科创板上市:拟募资4亿 范一与范浩兄弟为实控人
随机推荐
中金证券经理给的开户二维码办理股票开户安全吗?我想开个户
BN(Batch Normalization) 的理论理解以及在tf.keras中的实际应用和总结
Arrête d'être un bébé géant.
Application and Optimization Practice of 100 million level monthly live national karaoke feed service in Tencent cloud mongodb
360手机助手首家接入APP签名服务系统 助力隐私安全分发
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)
Android IO, a first-line Internet manufacturer, is a collection of real questions for senior Android interviews
Flutter 中 ValueNotifier<List<T>> 监听问题解决
回首望月
Treasure and niche cover PBR multi-channel mapping material website sharing
[Shandong University] information sharing for the first and second examinations of postgraduate entrance examination
Cause analysis of 12 MySQL slow queries
股票炒股注册开户有没有什么风险?安全吗?
网络爬虫2:抓取网易云音乐评论用户ID及主页地址
这个算BUG吗?乱填的字母是否可以关闭
如何用 SAP BTP 平台上的图形建模器创建一个 OData 服务
Implementation of collaborative filtering evolution version neuralcf and tensorflow2
CVPR 2022 | 美团技术团队精选论文解读
AI intelligent matting tool - hair can be seen
Module 5 operation