当前位置:网站首页>leetcode:152. Product maximum subarray [consider DP of two dimensions]
leetcode:152. Product maximum subarray [consider DP of two dimensions]
2022-06-26 21:42:00 【Review of the white speed Dragon King】
analysis
To find the subarray with the largest product
We use the maximum value that can be reached at present , The minimum value is enough
Concrete newmaxn = max(maxn * num, minn * minn, num)
newminn Empathy
Because it may be the smallest negative number multiplied by the current negative number is the smallest
So we can't simply solve it like the maximal sum of subarrays
Last record each maxn The maximum value of
ac code
class Solution:
def maxProduct(self, nums: List[int]) -> int:
# Maximum + The smallest array => Product maximum
maxn, minn, ans = 1, 1, -inf
for num in nums:
# Keep going
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
summary
Record the maximum and minimum values at the same time dp
边栏推荐
- VB.net类库(进阶——2 重载)
- Matrix derivation and its chain rule
- 【连载】说透运维监控系统01-监控系统概述
- How to analyze financial expenses
- 十大券商注册开户有没有什么风险?安全吗?
- 360 mobile assistant is the first to access the app signature service system to help distribute privacy and security
- Common concurrent testing tools and pressure testing methods
- In 2022, where will the medium and light-weight games go?
- 2022年,中轻度游戏出海路在何方?
- AI intelligent matting tool - hair can be seen
猜你喜欢
360 mobile assistant is the first to access the app signature service system to help distribute privacy and security
亿级月活全民K歌Feed业务在腾讯云MongoDB中的应用及优化实践
Dynamic parameter association using postman
Icml2022 | neurotoxin: a lasting back door to federal learning
记录一次Redis大Key的排查
大家都能看得懂的源码(一)ahooks 整体架构篇
VB.net类库——4给屏幕截图,裁剪
[LeetCode]-链表-2
YOLOv6:又快又准的目标检测框架开源啦
Introduction of classic wide & deep model and implementation of tensorflow 2 code
随机推荐
Android mediacodec hard coded H264 file (four), ByteDance Android interview
Application and Optimization Practice of 100 million level monthly live national karaoke feed service in Tencent cloud mongodb
Installation avec homebrew dans un environnement Mac OS [email protected]
Introduction to dependency injection in SAP Spartacus
ICML2022 | Neurotoxin:联邦学习的持久后门
传纸条【动态规划】
大家都能看得懂的源码(一)ahooks 整体架构篇
[solution] sword finger offer 15 Number of 1 in binary (C language)
Leetcode(122)——买卖股票的最佳时机 II
QT环境下配置Assimp库(MinGW编译器)
不同的子序列问题I
SAP commerce cloud project Spartacus getting started
Leetcode(452)——用最少数量的箭引爆气球
Leetcode question brushing: String 03 (Sword finger offer 05. replace space)
About appium trample pit: encountered internal error running command: error: cannot verify the signature of (solved)
YuMinHong: New Oriental does not have a reversal of falling and turning over, destroying and rising again
leetcode刷题:字符串06(实现 strStr())
MacOS環境下使用HomeBrew安裝[email protected]
Arrête d'être un bébé géant.
关于appium踩坑 :Encountered internal error running command: Error: Cannot verify the signature of (已解决)