当前位置:网站首页>Question 152: product maximum subarray
Question 152: product maximum subarray
2022-07-26 23:38:00 【Yingtai night snow】
Power button 152 topic : Product maximum subarray
Title Description
Give you an array of integers nums , Please find the non empty continuous subarray with the largest product in the array ( The subarray contains at least one number ), And returns the product of the subarray .
The answer to the test case is 32- position Integers .
Subarray Is a continuous subsequence of an array .
I/o sample
Input : nums = [2,3,-2,4]
Output : 6
explain : Subarray [2,3] There is a maximum product 6.
Input : nums = [-2,0,-1]
Output : 0
explain : The result can't be 2, because [-2,-1] It's not a subarray .
Solution 1 , Using dynamic programming method
// Using dynamic programming , First of all, we need to understand the transformation conditions of the state transition equation
// It's obvious , When there is a complex number , Insufficient state transition conditions , Therefore, you need to save the current corresponding maximum and minimum values
int maxProduct(vector<int>&nums)
{
if(nums.empty())
{
return 0;
}
int length=nums.size();
// Establish the state transition equation , Two dimensional state transition equation ,1 For the maximum , 0 Represents the minimum value
vector<vector<int>>dp(length,vector<int>(2));
// Initialization status , The initial maximum and minimum values are themselves
dp[0][0]=nums[0];
dp[0][1]=nums[0];
for(int i=1;i<length;i++)
{
// When the corresponding value is greater than 0 when
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]);
}
}
// Get the maximum
int res=dp[0][1];
for(int i=1;i<length;i++)
{
res=max(res,dp[i][1]);
}
return res;
}
Solution 2 , Optimize the space of Dynamic Planning
// Optimize the spatial complexity of dynamic programming
int maxProduct2(vector<int>nums)
{
// Save the current maximum , The minimum value
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;
// Considering the difference between positive and negative numbers , So set two values
maxValue=max(tempMax*nums[i],max(nums[i],tempMin*nums[i]));
minValue=min(tempMin*nums[i],min(nums[i],tempMax*nums[i]));
// Get the current maximum
res=max(maxValue,res);
}
return res;
}
边栏推荐
- MySQL random paging to get non duplicate data
- 【C语言】数组
- Several inventory terms often used in communication
- [H5 bottom scrolling paging loading]
- Import of MySQL data
- Disk expansion process and problems encountered in the virtual machine created by VMWare
- Vit:vision transformer super detailed with code
- 30、 Modern storage system (management database and distributed storage system)
- Application of workflow engine in vivo marketing automation | engine 03
- What is the reason for oom during redis shake synchronization in shake database?
猜你喜欢

第二部分—C语言提高篇_7. 结构体
![[shader realizes shine effect _shader effect Chapter 3]](/img/ea/6c14f682e6157a96c1877d99c9f7d3.png)
[shader realizes shine effect _shader effect Chapter 3]

Problems and solutions encountered in using nextline(), nextint() and next() in scanner

数据供应链的转型 协调一致走向成功的三大有效策略

Download win10 system image and create virtual machine on VMware virtual machine

Cheaper than seals, with a large space for shape explosion. Is there really no match for 200000 or so? Chang'an's new "King fried" is cost-effective

SQL Basics

MySQL 数据的导入

HCIA-R&S自用笔记(19)VLAN配置及实验、VLAN间路由

告别宽表,用 DQL 成就新一代 BI
随机推荐
【不积跬步无以至千里】统计日志指定时间段内的关键词
[Luogu] p2709 little B's inquiry
2022.7.26-----leetcode.1206
Part II - C language improvement_ 5. Bit operation
Real time voice quality monitoring
会议OA之我的会议
About statefulwidget, you have to know the principle and main points!
Problems and solutions encountered in using nextline(), nextint() and next() in scanner
第二部分—C语言提高篇_6. 多维数组
Science | University of Washington uses AI and structural prediction to design new proteins
第二部分—C语言提高篇_9. 链表
会议OA项目排座功能以及送审功能
研究阿尔茨海默病最经典的Nature论文涉嫌造假
华测RTK采集的GPX数据如何带属性转出kml、shp进行后续的管理和分析
np.transpose & np.expand_dims
Kingbasees SQL language reference manual of Jincang database (3.1.1.14. scope type)
[postgresql]postgresqlg use generate_ Series() function completes statistics
SQL Basics
科研太忙无法顾家?陈婷:人生不能只有一个支点
Easily implement seckill system with redis! (including code)