当前位置:网站首页>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
边栏推荐
- leetcode刷题:字符串01(反转字符串)
- leetcode刷题:字符串02( 反转字符串II)
- YOLOv6:又快又准的目標檢測框架開源啦
- 龙芯中科科创板上市:市值357亿 成国产CPU第一股
- Can compass open an account for stock trading? Is it safe?
- MacOS環境下使用HomeBrew安裝[email protected]
- Arrête d'être un bébé géant.
- lotus configurations
- Icml2022 | neurotoxin: a lasting back door to federal learning
- Treasure and niche cover PBR multi-channel mapping material website sharing
猜你喜欢

网络爬虫2:抓取网易云音乐评论用户ID及主页地址

Convolutional neural network (CNN) explanation and tensorflow2 code implementation

Treasure and niche cover PBR multi-channel mapping material website sharing

leetcode刷题:字符串05(剑指 Offer 58 - II. 左旋转字符串)

MATLAB and MySQL database connection and data exchange (based on ODBC)
![[protobuf] some pits brought by protobuf upgrade](/img/c1/01ba268c786c046d6fbc450a72ab45.png)
[protobuf] some pits brought by protobuf upgrade

Record a redis large key troubleshooting

AI intelligent matting tool - hair can be seen

卷积神经网络(CNN)详解及TensorFlow2代码实现

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?
随机推荐
12个MySQL慢查询的原因分析
CVPR 2022 | 美团技术团队精选论文解读
How to enable Hana cloud service on SAP BTP platform
Arrête d'être un bébé géant.
SAP Commerce Cloud 项目 Spartacus 入门
Chapter 2 construction of self defined corpus
Convolutional neural network (CNN) explanation and tensorflow2 code implementation
The source code that everyone can understand (I) the overall architecture of ahooks
Y48. Chapter III kubernetes from introduction to mastery -- pod status and probe (21)
俞敏洪:新东方并不存在倒下再翻身,摧毁又雄起的逆转
后台查找,如何查找网站后台
Treasure and niche cover PBR multi-channel mapping material website sharing
在哪个平台买股票开户最安全?求分享
[leetcode]- linked list-2
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?
SAP commerce cloud project Spartacus getting started
leetcode刷题:字符串03(剑指 Offer 05. 替换空格)
Matrix derivation and its chain rule
leetcode刷题:字符串01(反转字符串)
MacOS環境下使用HomeBrew安裝[email protected]