当前位置:网站首页>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
这里同时考虑正负两种情况
边栏推荐
- 传纸条【动态规划】
- Android IO, a first-line Internet manufacturer, is a collection of real questions for senior Android interviews
- Netease Yunxin officially joined the smart hospital branch of China Medical Equipment Association to accelerate the construction of smart hospitals across the country
- 网易云信正式加入中国医学装备协会智慧医院分会,为全国智慧医院建设加速...
- lotus configurations
- Talk about my remote work experience | community essay solicitation
- 在哪个平台买股票开户最安全?求分享
- Operator介绍
- 第2章 构建自定义语料库
- 茂莱光学科创板上市:拟募资4亿 范一与范浩兄弟为实控人
猜你喜欢
Record a redis large key troubleshooting
Establish a connection with MySQL
茂莱光学科创板上市:拟募资4亿 范一与范浩兄弟为实控人
MATLAB与Mysql数据库连接并数据交换(基于ODBC)
Test comparison of linear model LN, single neural network SNN, deep neural network DNN and CNN
【protobuf 】protobuf 昇級後帶來的一些坑
Listing of maolaiguang discipline on the Innovation Board: it is planned to raise 400million yuan. Fanyi and fanhao brothers are the actual controllers
leetcode刷题:字符串05(剑指 Offer 58 - II. 左旋转字符串)
【题解】剑指 Offer 15. 二进制中1的个数(C语言)
基于QT开发的线性代数初学者的矩阵计算器设计
随机推荐
VB.net类库(进阶版——1)
In 2022, where will the medium and light-weight games go?
MacOS環境下使用HomeBrew安裝[email protected]
fastadmin极光推送发送消息的时候registration_id多个用逗号分割后无效
「连续学习Continual learning, CL」最新2022研究综述
第2章 构建自定义语料库
Twenty five of offer - all paths with a certain value in the binary tree
DAST black box vulnerability scanner part 5: vulnerability scanning engine and service capability
2022年,中轻度游戏出海路在何方?
十大券商注册开户有没有什么风险?安全吗?
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)
curl: (35) LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection
DLA模型(分类模型+改进版分割模型) + 可变形卷积
【连载】说透运维监控系统01-监控系统概述
MacOS环境下使用HomeBrew安装[email protected]
Leetcode: String 04 (reverse the words in the string)
Introduction of classic wide & deep model and implementation of tensorflow 2 code
[Shandong University] information sharing for the first and second examinations of postgraduate entrance examination
中金证券经理给的开户二维码办理股票开户安全吗?我想开个户
【protobuf 】protobuf 昇級後帶來的一些坑