当前位置:网站首页>力扣152题:乘积最大子数组
力扣152题:乘积最大子数组
2022-07-26 22:25:00 【瀛台夜雪】
力扣152题:乘积最大子数组
题目描述
给你一个整数数组 nums ,请你找出数组中乘积最大的非空连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。
测试用例的答案是一个 32-位 整数。
子数组 是数组的连续子序列。
输入输出样例
输入: nums = [2,3,-2,4]
输出: 6
解释: 子数组 [2,3] 有最大乘积 6。
输入: nums = [-2,0,-1]
输出: 0
解释: 结果不能为 2, 因为 [-2,-1] 不是子数组。
解法一,使用动态规划方法
//使用动态规划的方法,首先需要了解状态转移方程的转换条件
//可以明显的看出,当存在复数的时候,状态转移条件不够用,因此需要保存当前对应的最大值和最小值
int maxProduct(vector<int>&nums)
{
if(nums.empty())
{
return 0;
}
int length=nums.size();
//建立状态转移方程,二维的状态转移方程,1代表最大值, 0代表最小值
vector<vector<int>>dp(length,vector<int>(2));
//初始化状态,初始的最大最小值都是本身
dp[0][0]=nums[0];
dp[0][1]=nums[0];
for(int i=1;i<length;i++)
{
//当对应的值大于0时
if(nums[i]>=0)
{
dp[i][1]=max(nums[i],dp[i-1][1]*nums[i]);
dp[i][0]=min(nums[i],dp[i-1][0]*nums[i]);
}
else
{
dp[i][1]=max(nums[i],dp[i-1][0]*nums[i]);
dp[i][0]=min(nums[i],dp[i-1][1]*nums[i]);
}
}
//获得最大值
int res=dp[0][1];
for(int i=1;i<length;i++)
{
res=max(res,dp[i][1]);
}
return res;
}
解法二,优化动态规划的空间
//优化动态规划的空间复杂度
int maxProduct2(vector<int>nums)
{
//保存当前的最大,最小的值
int maxValue=nums[0];
int minValue=nums[0];
int res=nums[0];
for(int i=1;i<nums.size();i++)
{
int tempMax=maxValue;
int tempMin=minValue;
//考虑到正负数的不同,因此设置两个值
maxValue=max(tempMax*nums[i],max(nums[i],tempMin*nums[i]));
minValue=min(tempMin*nums[i],min(nums[i],tempMax*nums[i]));
//获取当前的最大值
res=max(maxValue,res);
}
return res;
}
边栏推荐
- [H5 bottom scrolling paging loading]
- Use Arthas to locate online problems
- Typescript notes
- Counter attack dark horse: devdbops training, give you the best courses!
- KT6368A蓝牙芯片开发注意事项以及问题集锦--长期更新
- Signal debugging document developed by car
- My SQL is OK. Why is it still so slow? MySQL locking rules
- HCIA-R&S自用笔记(20)VLAN综合实验、GVRP
- Public cloud security and compliance considerations
- Hcia-r & s self use notes (20) VLAN comprehensive experiment, GVRP
猜你喜欢
![[H5 bottom scrolling paging loading]](/img/2c/fb8dd8a7d985392450ad7d3b70016c.png)
[H5 bottom scrolling paging loading]

Use Arthas to locate online problems

Do you know the common core types of magnetic ring inductors?
![[postgresql]postgresqlg use generate_ Series() function completes statistics](/img/62/893986eb97a61f4e9ef32abc8d2a90.png)
[postgresql]postgresqlg use generate_ Series() function completes statistics

Silicon Valley class lesson 7 - Tencent cloud on demand management module (2)

Esmfold: a new breakthrough in protein structure prediction after alphafold2

Eureka basic use

【MySQL】CentOS 7.9安装、使用MySQL-5.7.39二进制版

Counter attack dark horse: devdbops training, give you the best courses!

研究阿尔茨海默病最经典的Nature论文涉嫌造假
随机推荐
DAO:OP 代币和不可转让的 NFT 致力于建立新的数字民主
Boss; Can flick CDC Oracle finish reading the full amount of data, just like directly fetching data from the database
Ribbon load balancing
Basic operations of objects
Security team: Recently, there is an rce vulnerability in the COREMAIL email client of windows, which may lead to the disclosure of the private key of the wallet
[shader realizes shine effect _shader effect Chapter 3]
Silicon Valley class lesson 7 - Tencent cloud on demand management module (2)
Part II - C language improvement_ 5. Bit operation
Dao:op token and non transferable NFT are committed to building a new digital democracy
Application of workflow engine in vivo marketing automation | engine 03
2022年物联网行业有哪些用例?
Easily implement seckill system with redis! (including code)
Introduction to the use of Jerry downloader forced download tool_ Ac695n696nad14ad15 full range support
Three effective strategies for the transformation of data supply chain to be coordinated and successful
HCIA-R&S自用笔记(18)园区网架构基础、交换机工作原理、VLAN原理
第二部分—C语言提高篇_5. 位运算
The interviewer asked: this point of JS
Why am I still writing articles on CSDN? A journey of accompanying learning.
Typescript stage learning
Machine learning notes - building recommendation system (3) six research directions of deep recommendation system