当前位置:网站首页>leetcode:152. 乘积最大子数组【考虑两个维度的dp】
leetcode:152. 乘积最大子数组【考虑两个维度的dp】
2022-06-26 21:39:00 【白速龙王的回眸】

分析
为了找到乘积最大的子数组
我们用当前能达到的最大值,最小值即可
具体的newmaxn = max(maxn * num, minn * minn, num)
newminn同理
因为可能是最小的负数乘当前的负数是最小
所以就不能简单地像子数组最大和这样解决
最后记录每个maxn的最大值即可
ac code
class Solution:
def maxProduct(self, nums: List[int]) -> int:
# 最大 + 最小子数组 => 乘积最大
maxn, minn, ans = 1, 1, -inf
for num in nums:
# 保持连续
maxn1, minn1 = max(maxn * num, minn * num, num), min(maxn * num, minn * num, num)
maxn = maxn1
minn = minn1
ans = max(maxn, ans)
#print(minn, maxn)
return ans
总结
同时记录最大值和最小值的dp
边栏推荐
- The source code that everyone can understand (I) the overall architecture of ahooks
- 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)
- Matrix calculator design for beginners of linear algebra based on Qt development
- 第2章 构建自定义语料库
- Introduction of classic wide & deep model and implementation of tensorflow 2 code
- 「连续学习Continual learning, CL」最新2022研究综述
- BN(Batch Normalization) 的理论理解以及在tf.keras中的实际应用和总结
- Is there any risk in opening a mobile stock registration account? Is it safe?
- leetcode刷题:字符串02( 反转字符串II)
- 基于Qt实现的“合成大西瓜”小游戏
猜你喜欢

俞敏洪:新东方并不存在倒下再翻身,摧毁又雄起的逆转

VB.net类库(进阶——2 重载)

What are the accounting elements

Vi/vim editor

360手机助手首家接入APP签名服务系统 助力隐私安全分发

Sword finger offer 12 Path in matrix

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

MATLAB and MySQL database connection and data exchange (based on ODBC)

360 mobile assistant is the first to access the app signature service system to help distribute privacy and security

【 protobuf 】 quelques puits causés par la mise à niveau de protobuf
随机推荐
剑指 Offer II 098. 路径的数目 / 剑指 Offer II 099. 最小路径之和
PostgreSQL notes
回首望月
[protobuf] some pits brought by protobuf upgrade
Many gravel 3D material mapping materials can be obtained with one click
AI智能抠图工具--头发丝都可见
leetcode刷题:字符串02( 反转字符串II)
Sword finger offer 12 Path in matrix
Treasure and niche cover PBR multi-channel mapping material website sharing
MATLAB与Mysql数据库连接并数据交换(基于ODBC)
leetcode刷题:字符串01(反转字符串)
亿级月活全民K歌Feed业务在腾讯云MongoDB中的应用及优化实践
C: Reverse linked list
诗尼曼家居冲刺A股:年营收近12亿 红星美凯龙与居然之家是股东
random_normal_initializer 使用
网易云信正式加入中国医学装备协会智慧医院分会,为全国智慧医院建设加速...
Redis + Guava 本地缓存 API 组合,性能炸裂!
Record a redis large key troubleshooting
Netease Yunxin officially joined the smart hospital branch of China Medical Equipment Association to accelerate the construction of smart hospitals across the country
YuMinHong: New Oriental does not have a reversal of falling and turning over, destroying and rising again