当前位置:网站首页>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
边栏推荐
- 后台查找,如何查找网站后台
- Usage of MGrid in numpy
- 协同过滤进化版本NeuralCF及tensorflow2实现
- numpy中mgrid的用法
- 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)
- Chapter 2 construction of self defined corpus
- Leetcode question brushing: String 02 (reverse string II)
- [Bayesian classification 4] Bayesian network
- leetcode刷题:字符串04(颠倒字符串中的单词)
- How SAP Spartacus default routing configuration works
猜你喜欢

Y48. Chapter III kubernetes from introduction to mastery -- pod status and probe (21)

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

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

基于QT实现简单的连连看小游戏

Many gravel 3D material mapping materials can be obtained with one click

Yonghui released the data of Lantern Festival: the sales of Tangyuan increased significantly, and several people's livelihood products increased by more than 150%

AI智能抠图工具--头发丝都可见

VB.net类库(进阶版——1)

Android IO, a first-line Internet manufacturer, is a collection of real questions for senior Android interviews

经典Wide & Deep模型介绍及tensorflow 2代码实现
随机推荐
MacOS環境下使用HomeBrew安裝[email protected]
财务费用分析怎么分析
Redis + guava local cache API combination, performance burst!
【连载】说透运维监控系统01-监控系统概述
Y48. Chapter III kubernetes from introduction to mastery -- pod status and probe (21)
SAP Spartacus 中的依赖注入 Dependency Injection 介绍
Godson China Science and technology innovation board is listed: the market value is 35.7 billion yuan, becoming the first share of domestic CPU
KDD2022 | 基于知识增强提示学习的统一会话推荐系统
SAP Commerce Cloud 项目 Spartacus 入门
基于QT开发的线性代数初学者的矩阵计算器设计
lotus configurations
What are the accounting elements
YOLOv6:又快又准的目標檢測框架開源啦
AI intelligent matting tool - hair can be seen
【protobuf 】protobuf 升级后带来的一些坑
Common concurrent testing tools and pressure testing methods
模块五作业
DLA模型(分类模型+改进版分割模型) + 可变形卷积
Icml2022 | neurotoxin: a lasting back door to federal learning
BN(Batch Normalization) 的理论理解以及在tf.keras中的实际应用和总结