当前位置:网站首页>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
边栏推荐
- Stop being a giant baby
- 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?
- MacOS環境下使用HomeBrew安裝[email protected]
- Android mediacodec hard coded H264 file (four), ByteDance Android interview
- leetcode刷题:字符串02( 反转字符串II)
- 【题解】剑指 Offer 15. 二进制中1的个数(C语言)
- CVPR 2022 | 美团技术团队精选论文解读
- Y48. Chapter III kubernetes from introduction to mastery -- pod status and probe (21)
- What are the accounting elements
- The postgraduate entrance examination in these areas is crazy! Which area has the largest number of candidates?
猜你喜欢

Leetcode: String 04 (reverse the words in the string)

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

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

【连载】说透运维监控系统01-监控系统概述

QT环境下配置Assimp库(MinGW编译器)

【protobuf 】protobuf 升级后带来的一些坑

Simple Lianliankan games based on QT

leetcode刷题:字符串06(实现 strStr())

Kdd2022 𞓜 unified session recommendation system based on knowledge enhancement prompt learning

2022年,中轻度游戏出海路在何方?
随机推荐
[protobuf] some pits brought by protobuf upgrade
YuMinHong: New Oriental does not have a reversal of falling and turning over, destroying and rising again
同花顺注册开户有没有什么风险?安全吗?
Listing of maolaiguang discipline on the Innovation Board: it is planned to raise 400million yuan. Fanyi and fanhao brothers are the actual controllers
俞敏洪:新东方并不存在倒下再翻身,摧毁又雄起的逆转
线性模型LN、单神经网络SNN、深度神经网络DNN与CNN测试对比
Web crawler 2: crawl the user ID and home page address of Netease cloud music reviews
在哪家证券公司开户最方便最安全可靠
random_ normal_ Initializer uses
Is there any risk in opening a securities registration account? Is it safe?
QT环境下配置Assimp库(MinGW编译器)
聊聊我的远程工作体验 | 社区征文
Looking back at the moon
leetcode刷题:字符串01(反转字符串)
记录一次Redis大Key的排查
Implementation of collaborative filtering evolution version neuralcf and tensorflow2
y48.第三章 Kubernetes从入门到精通 -- Pod的状态和探针(二一)
Vi/vim editor
Operator介绍
Leetcode: String 04 (reverse the words in the string)