当前位置:网站首页>152. 乘积最大子数组
152. 乘积最大子数组
2022-07-25 16:27:00 【ZNineSun】
打卡!!!每日一题
今天继续为大家分享一道动态规划类型的题目。
题目描述:
给你一个整数数组 nums ,请你找出数组中乘积最大的非空连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。
测试用例的答案是一个 32-位 整数。
子数组 是数组的连续子序列。
题目示例:
刚开始看到这道题,对其很是轻蔑,想着这不就是一道很入门的动态规划题目吗,还是老规矩,找状态转移方程。
即dp[i]:表示到第i个结点对应的最大连续子数组的乘积。
则有dp[i]=max{nums[i],nums[i]*dp[i-1]}
以题目示例1为例:
初始值: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
于是我很快写入了以下代码:
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]);
}
//寻找最大值
int max = dp[0];
for (int i = 1; i < length; i++) {
max = Math.max(max, dp[i]);
}
return max;
}
可是在我提交测试用例的时候,发现
然后我才意识到我少考虑了一种情况,因为按照我上面的思路
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
最后得道最大值3
在上面的这个过程中,我忽略了负值的情况,也就是说前面可能是负值,后面又乘了一个负值之后,就会变成一个较大的值。
解决办法也很简单:我们只需要原来dp的基础上,在接着比较一个最小值和nums[i]相乘即可,那么我们就不得不在引入一个dp_min来存放最小值,于是有:
- dp_max[i]:存放和nums[i]相乘的最大连续子数组的值
- dp_min[i]:存放和nums[i]相乘的最小连续子数组的值
对应的状态转移方程如下:
- 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]}
初始值:
- dp_max[0]=nums[0]
- dp_min[0]=nums[0]
对应代码如下:
public int maxProduct(int[] nums) {
int length = nums.length;
if (length <= 1) {
return nums[0];
}
int[] dp_max = new int[length];//存放最大值
int[] dp_min = new int[length];//存放最小值
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;
}
边栏推荐
- Product dynamics - Android 13 high-efficiency adaptation new upgrade
- 共享锁(Shared Lock)
- What is a physical firewall? What's the effect?
- MySQL table write lock
- Release of v6.5.1/2/3 series of versions of Xingyun housekeeper: the ability of database OpenAPI continues to be strengthened
- 递归菜单查询(递归:自己查自己)
- easyui入门
- Differences between cookies, cookies and sessions
- 测试框架-unittest-命令行操作、断言方法
- 使用 Terraform 在 AWS 上快速部署 MQTT 集群
猜你喜欢

QT ListView 列表显示组件笔记

easyui datagrid控件使用

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

Food safety - do you really understand the ubiquitous frozen food?

leetcode:528. 按权重随机选择【普通随机失效 + 要用前缀和二分】

IaaS基础架构云 —— 云网络

Fastadmin TP installation uses Baidu rich text editor ueeditor

Crazy God redis notes 12

Use huggingface to quickly load pre training models and datasets in moment pool cloud

Baidu rich text editor ueeditor single image upload cross domain
随机推荐
【读书会第13期】+FFmpeg开源项目
从业务需求出发,开启IDC高效运维之路
使用 Terraform 在 AWS 上快速部署 MQTT 集群
Sum arrays with recursion
自定义mvc项目登录注册和树形菜单
MySQL table read lock
Google Earth Engine——全球建筑物GlobalMLBuildingFootprints矢量集合下载
【ZeloEngine】反射系统填坑小结
【MySQL篇】一文带你初识数据库
[image denoising] image denoising based on bicube interpolation and sparse representation matlab source code
Quickly deploy mqtt clusters on AWS using terraform
柏睿数据加入阿里云PolarDB开源数据库社区
ILSSI认证|六西格玛DMAIC的历程
Mqtt x cli officially released: powerful and easy-to-use mqtt 5.0 command line tool
Exploration of 6-wire SPI transmission mode
What is a physical firewall? What's the effect?
权限管理-角色分配菜单
Differences between cookies, cookies and sessions
QT ListView 列表显示组件笔记
Fastadmin TP installation uses Baidu rich text editor ueeditor