当前位置:网站首页>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;
}
边栏推荐
- 【面试:并发篇26:多线程:两阶段终止模式】volatile版本
- HCIA-R&S自用笔记(19)VLAN配置及实验、VLAN间路由
- ESMFold: AlphaFold2之后蛋白质结构预测的新突破
- Part II - C language improvement_ 7. Structure
- Concept of functional interface & definition and use of functional interface
- HCIA-R&S自用笔记(18)园区网架构基础、交换机工作原理、VLAN原理
- np.transpose & np.expand_dims
- Six challenges facing enterprise data governance!
- 什么是 Base64 ?
- 大疆智图、CC生产了多份数据,如何合并为一份在图新地球进行加载
猜你喜欢

Three person management of system design

MySQL random paging to get non duplicate data

SQL 基础知识

1. Configuration environment and project creation

In simple terms, cchart's daily lesson - Lesson 59 of happy high school 4 comes to the same end by different ways, and the C code style of the colorful interface library
![[shader realizes swaying effect _shader effect Chapter 4]](/img/ab/bdbc4a0f297541b532af81a49e2633.png)
[shader realizes swaying effect _shader effect Chapter 4]
![[shader realizes shine effect _shader effect Chapter 3]](/img/ea/6c14f682e6157a96c1877d99c9f7d3.png)
[shader realizes shine effect _shader effect Chapter 3]
![[shaders realize distorted outline effect _shader effect Chapter 2]](/img/b3/ab28039cce2521ff1d59f0de6bf852.png)
[shaders realize distorted outline effect _shader effect Chapter 2]

会议OA之我的会议

Disk expansion process and problems encountered in the virtual machine created by VMWare
随机推荐
How to use data pipeline to realize test modernization
ESMFold: AlphaFold2之后蛋白质结构预测的新突破
The NFT market pattern has not changed. Can okaleido set off a new round of waves?
Vit:vision transformer super detailed with code
The nature and proof of the center of gravity of [mathematics] tree
公有云安全性和合规性方面的考虑事项
[shader realizes shine effect _shader effect Chapter 3]
P5469 [noi2019] robot (Lagrange interpolation, interval DP)
【C语言】经典的递归问题
Typescript notes
Kingbasees SQL language reference manual of Jincang database (3.1.1.14. scope type)
Part II - C language improvement_ 13. Recursive function
Go uses flag package to parse command line parameters
Several inventory terms often used in communication
Machine learning notes - building recommendation system (3) six research directions of deep recommendation system
第二部分—C语言提高篇_10. 函数指针和回调函数
30、 Modern storage system (management database and distributed storage system)
Re understand the life world and ourselves
At 12:00 on July 17, 2022, the departure of love life on June 28 was basically completed, and it needs to rebound
股票开户佣金是否可以调整?手机上开户安不安全