当前位置:网站首页>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
边栏推荐
猜你喜欢

回首望月

Redis + guava local cache API combination, performance burst!
![[serial] shuotou O & M monitoring system 01 overview of monitoring system](/img/b2/bc75a4d0c8d98056d93ba99b3e6193.png)
[serial] shuotou O & M monitoring system 01 overview of monitoring system

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

YOLOv6:又快又准的目标检测框架开源啦

线性模型LN、单神经网络SNN、深度神经网络DNN与CNN测试对比

Vi/vim editor

leetcode刷题:字符串04(颠倒字符串中的单词)

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

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)
随机推荐
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)
线性模型LN、单神经网络SNN、深度神经网络DNN与CNN测试对比
[leetcode]- linked list-2
基于QT开发的线性代数初学者的矩阵计算器设计
PostgreSQL notes
Listing of maolaiguang discipline on the Innovation Board: it is planned to raise 400million yuan. Fanyi and fanhao brothers are the actual controllers
【题解】剑指 Offer 15. 二进制中1的个数(C语言)
SAP Commerce Cloud 项目 Spartacus 入门
CVPR 2022 | 美团技术团队精选论文解读
Leetcode question brushing: String 05 (Sword finger offer 58 - ii. left rotation string)
Kdd2022 𞓜 unified session recommendation system based on knowledge enhancement prompt learning
curl: (35) LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection
【连载】说透运维监控系统01-监控系统概述
The network connection is disconnected. Please refresh and try again
How to analyze financial expenses
Matrix calculator design for beginners of linear algebra based on Qt development
Leetcode(122)——买卖股票的最佳时机 II
QT环境下配置Assimp库(MinGW编译器)
第2章 构建自定义语料库
Talk about my remote work experience | community essay solicitation