当前位置:网站首页>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;
}
边栏推荐
- 01. A simpler way to deliver a large number of props
- Permission management - role assignment menu
- MyBaits
- MySQL table read lock
- The annualized interest rate of treasury bonds is too low. Is there a financial product with a higher annualized interest rate than the reverse repurchase of treasury bonds?
- 02. 将参数props限制在一个类型的列表中
- 【小5聊】公众号排查<该公众号提供的服务出现故障,请稍后>
- Understanding service governance in distributed development
- Record locks
- 聊聊如何用 Redis 实现分布式锁?
猜你喜欢

【ZeloEngine】反射系统填坑小结

城市燃气安全再拉警钟,如何防患于未“燃”?

How does win11's own drawing software display the ruler?

使用 Terraform 在 AWS 上快速部署 MQTT 集群

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

Which led display manufacturer is better

Mqtt x cli officially released: powerful and easy-to-use mqtt 5.0 command line tool

自定义mvc项目登录注册和树形菜单

Cookie、cookie与session区别

MyBaits
随机推荐
Solve win10 disk occupation of 100%
哪个led显示屏厂家更好
Use huggingface to quickly load pre training models and datasets in moment pool cloud
Promise date
MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具
Ilssi certification | the course of Six Sigma DMAIC
Win11自带画图软件怎么显示标尺?
MySQL explicit lock
阿唐的小帮手
邮件的收发的展现逻辑之收件箱发件箱以及回复断链的问题
【故障诊断】基于贝叶斯优化支持向量机的轴承故障诊断附matlab代码
MySQL显式锁
Permission management - role assignment menu
国债年化利率太低了,有比国债逆回购年化利率还要高的理财产品吗?
测试框架-unittest-测试套件、结果输出到文件
食品安全丨无处不在的冷冻食品,你真的了解吗?
【云驻共创】探秘GaussDB如何助力工商银行打造金融核心数据
Wechat applet does not use plug-ins, rendering videos in rich text, image adaptation, plus version
Typescript learning 2 - Interface
自定义mvc项目登录注册和树形菜单