当前位置:网站首页>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;
}
边栏推荐
- 02. Limit the parameter props to a list of types
- Simple rotation map and hamster beating
- Fastadmin TP installation uses Baidu rich text editor ueeditor
- MySQL global lock
- Win11动态磁贴没了?Win11中恢复动态磁贴的方法
- MYSQL导入sqllite表格的两种方法
- MySQL explicit lock
- Release of v6.5.1/2/3 series of versions of Xingyun housekeeper: the ability of database OpenAPI continues to be strengthened
- Attachment handling of SAP Fiori
- Is the win11 dynamic tile gone? Method of restoring dynamic tile in Win 11
猜你喜欢

Emqx cloud update: more parameters are added to log analysis, which makes monitoring, operation and maintenance easier

leetcode:6127. 优质数对的数目【位运算找规律 + 两数之和大于等于k + 二分】

Which led display manufacturer is better

tkinter模块高级操作(一)—— 透明按钮、透明文本框、自定义按钮及自定义文本框

进程之间的通信(管道详解)

测试驱动开发(TDD)在线练功房 | 9月17日开课

【故障诊断】基于贝叶斯优化支持向量机的轴承故障诊断附matlab代码

终极套娃 2.0 | 云原生交付的封装

【图像隐藏】基于混合 DWT-HD-SVD 的数字图像水印方法技术附matlab代码

复旦大学EMBA同学同行专题:始终将消费者的价值放在最重要的位置
随机推荐
[wechat applet] detailed explanation of applet host environment
Test Driven Development (TDD) online practice room | classes open on September 17
Waterfall flow layout
LVGL 7.11 tileview界面循环切换
Product upgrade observation station in June
Sum arrays with recursion
Paper notes: highly accurate protein structure prediction with alphafold (alphafold 2 & appendix)
Win11动态磁贴没了?Win11中恢复动态磁贴的方法
MySQL之联表查询、常用函数、聚合函数
泰雷兹推出解决方案,助力SAP客户控制云端数据
进程之间的通信(管道详解)
测试框架-unittest-测试套件、结果输出到文件
Visual studio 2022 view class diagram
Simple rotation map and hamster beating
柏睿数据加入阿里云PolarDB开源数据库社区
【小5聊】公众号排查<该公众号提供的服务出现故障,请稍后>
Golang review summary
Typescript learning 1 - data types
fastadmin tp 安装使用百度富文本编辑器UEditor
C# 音乐