当前位置:网站首页>152. Product maximum subarray
152. Product maximum subarray
2022-07-25 16:28:00 【ZNineSun】
Clock in !!! A daily topic
Today, I will continue to share with you Dynamic programming Type of topic .
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 .
Title Example :
At first, I saw this problem , Very contemptuous of it , Thinking this is a very basic dynamic planning problem , As usual , Find the state transition equation .
namely dp[i]: To i The product of the largest continuous subarray corresponding to nodes .
Then there are dp[i]=max{nums[i],nums[i]*dp[i-1]}
Take the topic as an example 1 For example :
Initial value :dp[0]=nums[0]=2;
dp[1]=max{nums[1],nums[1]*dp[0]}=6
dp[2]=max{nums[2],nums[2]*dp[1]}=-2
dp[3]=max{nums[3],nums[3]*dp[2]}=4
So I quickly wrote the following code :
public int maxProduct(int[] nums) {
int length = nums.length;
if (length <= 1) {
return nums[0];
}
int[] dp = new int[length];
dp[0] = nums[0];
for (int i = 1; i < length; i++) {
dp[i] = Math.max(nums[i], dp[i - 1] * nums[i]);
}
// Search for maximum
int max = dp[0];
for (int i = 1; i < length; i++) {
max = Math.max(max, dp[i]);
}
return max;
}
But when I submit test cases , Find out 
Then I realized that I had neglected one situation , Because according to my idea above
dp[0]=nums[0]=-2;
dp[1]=max{nums[1],nums[1]*dp[0]}=3
dp[2]=max{nums[2],nums[2]*dp[1]}=-4
Finally, the maximum value of Tao 3
In the above process , I ignored the negative value , That is to say, the front may be negative , Then multiply by a negative value , It will become a larger value .
The solution is simple : We just need the original dp On the basis of , Then compare a minimum value with nums[i] Multiply by , Then we have to introduce one dp_min To store the minimum , So there is :
- dp_max[i]: Storage and nums[i] Multiply Maximum Values of contiguous subarrays
- dp_min[i]: Storage and nums[i] Multiply Minimum Values of contiguous subarrays
The corresponding state transition equation is as follows :
- dp_max[i]=max{nums[i],dp_max[i - 1] * nums[i], dp_min[i - 1] * nums[i])}
- dp_min[i]=min{nums[i],dp_max[i - 1] * nums[i], dp_min[i - 1] * nums[i]}
Initial value :
- dp_max[0]=nums[0]
- dp_min[0]=nums[0]
The corresponding code is as follows :
public int maxProduct(int[] nums) {
int length = nums.length;
if (length <= 1) {
return nums[0];
}
int[] dp_max = new int[length];// Storage maximum
int[] dp_min = new int[length];// Storage minimum
dp_max[0] = nums[0];
dp_min[0] = nums[0];
for (int i = 1; i < length; i++) {
dp_max[i] = Math.max(nums[i], Math.max(dp_max[i - 1] * nums[i], dp_min[i - 1] * nums[i]));
dp_min[i] = Math.min(nums[i], Math.min(dp_max[i - 1] * nums[i], dp_min[i - 1] * nums[i]));
}
int ans = dp_max[0];
for (int i = 1; i < length; i++) {
ans = Math.max(ans, dp_max[i]);
}
return ans;
}
边栏推荐
- [image hiding] digital image watermarking method technology based on hybrid dwt-hd-svd with matlab code
- 首页门户分类查询
- 80篇国产数据库实操文档汇总(含TiDB、达梦、openGauss等)
- fastadmin tp 安装使用百度富文本编辑器UEditor
- Pagehelper.startpage is not effective
- Typescript learning 1 - data types
- MySQL-自增锁
- 百度富文本编辑器UEditor单张图片上传跨域
- 如何安装govendor并打开项目
- [fault diagnosis] bearing fault diagnosis based on Bayesian optimization support vector machine with matlab code
猜你喜欢
![Leetcode:154. find the minimum value II in the rotation sort array [about the middle and rear positioning dichotomy of the rotation sort array]](/img/03/54a2d82a17cd07374dc0aedacd7b11.png)
Leetcode:154. find the minimum value II in the rotation sort array [about the middle and rear positioning dichotomy of the rotation sort array]

Is the win11 dynamic tile gone? Method of restoring dynamic tile in Win 11

Breakthrough in core technology of the large humanoid Service Robot Walker x

食品安全丨无处不在的冷冻食品,你真的了解吗?

如何构建面向海量数据、高实时要求的企业级OLAP数据引擎?

今天去 OPPO 面试,被问麻了

从业务需求出发,开启IDC高效运维之路

Understanding service governance in distributed development

如何安装govendor并打开项目

leetcode:528. 按权重随机选择【普通随机失效 + 要用前缀和二分】
随机推荐
01.一个更简单的方法来传递大量的props
【图像隐藏】基于混合 DWT-HD-SVD 的数字图像水印方法技术附matlab代码
Is the win11 dynamic tile gone? Method of restoring dynamic tile in Win 11
Register service instances in ngmodule through dependency injection
ILSSI认证|六西格玛DMAIC的历程
Typescript learning 1 - data types
进程之间的通信(管道详解)
easyui入门
如何安装govendor并打开项目
MySQL table read lock
Shared lock
Food safety - do you really understand the ubiquitous frozen food?
百度富文本编辑器UEditor单张图片上传跨域
可验证随机函数 VRF
用递归进行数组求和
食品安全丨无处不在的冷冻食品,你真的了解吗?
权限管理-删除菜单(递归)
墨天轮高分技术文档分享——数据库安全篇(共48个)
easyui下拉框,增加以及商品的上架,下架
MYSQL导入sqllite表格的两种方法