当前位置:网站首页>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
边栏推荐
- y48.第三章 Kubernetes从入门到精通 -- Pod的状态和探针(二一)
- 基于启发式搜索的一字棋
- leetcode刷题:字符串05(剑指 Offer 58 - II. 左旋转字符串)
- Introduction to dependency injection in SAP Spartacus
- Introduction of classic wide & deep model and implementation of tensorflow 2 code
- Android mediacodec hard coded H264 file (four), ByteDance Android interview
- Final part of web crawler: send directional messages to 100000 Netease cloud users
- How to create an OData service with the graphical modeler on the sap BTP platform
- Godson China Science and technology innovation board is listed: the market value is 35.7 billion yuan, becoming the first share of domestic CPU
- 指南针能开户炒股吗?安全吗?
猜你喜欢

诗尼曼家居冲刺A股:年营收近12亿 红星美凯龙与居然之家是股东

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

leetcode刷题:字符串01(反转字符串)

龙芯中科科创板上市:市值357亿 成国产CPU第一股

基于QT开发的线性代数初学者的矩阵计算器设计

Godson China Science and technology innovation board is listed: the market value is 35.7 billion yuan, becoming the first share of domestic CPU
![[protobuf] some pits brought by protobuf upgrade](/img/c1/01ba268c786c046d6fbc450a72ab45.png)
[protobuf] some pits brought by protobuf upgrade

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

Matrix derivation and its chain rule

Hands on deep learning pytorch version 3 - Data Preprocessing
随机推荐
Treasure and niche cover PBR multi-channel mapping material website sharing
[Bayesian classification 4] Bayesian network
模块五作业
[Bayesian classification 2] naive Bayesian classifier
Student information management system based on SSH Framework
Matrix derivation and its chain rule
Simple Lianliankan games based on QT
网易云信正式加入中国医学装备协会智慧医院分会,为全国智慧医院建设加速...
2022年,中轻度游戏出海路在何方?
KDD2022 | 基于知识增强提示学习的统一会话推荐系统
SAP Spartacus 中的依赖注入 Dependency Injection 介绍
Application and Optimization Practice of 100 million level monthly live national karaoke feed service in Tencent cloud mongodb
Leetcode: hash table 08 (sum of four numbers)
YOLOv6:又快又准的目标检测框架开源啦
In 2022, where will the medium and light-weight games go?
Module 5 operation
random_normal_initializer 使用
不同的子序列问题I
BN(Batch Normalization) 的理论理解以及在tf.keras中的实际应用和总结
Is there any risk in opening a mobile stock registration account? Is it safe?