当前位置:网站首页>152. 乘积最大子数组-动态规划
152. 乘积最大子数组-动态规划
2022-07-25 23:21:00 【Mr Gao】
152. 乘积最大子数组
给你一个整数数组 nums ,请你找出数组中乘积最大的非空连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。
测试用例的答案是一个 32-位 整数。
子数组 是数组的连续子序列。
示例 1:
输入: nums = [2,3,-2,4]
输出: 6
解释: 子数组 [2,3] 有最大乘积 6。
示例 2:
输入: nums = [-2,0,-1]
输出: 0
解释: 结果不能为 2, 因为 [-2,-1] 不是子数组。
这题挺有意思的,解题代码如下:
int maxProduct(int* nums, int numsSize){
int dp[numsSize+1][2];
dp[1][0]=nums[0];
dp[1][1]=nums[0];
int maxt=dp[1][0];
for(int i=2;i<=numsSize;i++){
int max=nums[i-1]*dp[i-1][0];
int min=nums[i-1]*dp[i-1][1];
// printf("--min max %d %d",min, max);
if(max<min){
int t=min;
min=max;
max=t;
}
dp[i][0]=fmax(nums[i-1],max);
if(dp[i][0]>maxt){
maxt=dp[i][0];
}
dp[i][1]=fmin(nums[i-1],min);
// printf("min max %d %d",dp[i][1], dp[i][0]);
}
return maxt;
}
边栏推荐
- PHP binary array is sorted by a field in it
- Which securities company should a novice choose to open an account? Is it safe?
- The fifth article in the series of radar Fundamentals: the function of radar modulation style
- Idea sets get and set templates to solve the naming problem of boolean type fields
- Source code of wechat applet for discerning flowers and plants / source code of wechat applet for discerning plants
- Flight control implementation of four rotor aircraft "suggestions collection"
- WebMvcConfigurationSupport
- POI special effects Market Research
- PyTorch的数据输入格式要求及转换
- 加拿大EE通道
猜你喜欢

自定义mvc原理

wordpress去掉网站发布时间

VisualBox启动虚拟机报错:The VM session was closed before any attempt to power it on.
![[interface performance optimization] reasons for index failure and how to optimize SQL](/img/b9/64058c823c4497ac36bfb62a101816.jpg)
[interface performance optimization] reasons for index failure and how to optimize SQL

Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions

多模态——Deep Multi-Modal Sets

How does PHP remove an element from an array based on the key value

XXE&XML-外部实体注入-利用和绕过

chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted

Custom MVC principle
随机推荐
Implementation of mesh parameterized least squares conformal maps (3D mesh mapping to 2D plane)
ETL工具(数据同步) 二
Check code generation
[QNX Hypervisor 2.2用户手册]9.8 load
The fifth article in the series of radar Fundamentals: the function of radar modulation style
Discuz magazine / news report template (jeavi_line) utf8 GBK / DZ template download
[QNX hypervisor 2.2 user manual]9.7 generate
[QNX Hypervisor 2.2用户手册]9.7 generate
How does PHP remove an element from an array based on the key value
chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted
新手开户选择哪个券商公司好呢?安全吗
Explain in detail the addition (+) operation in JS, basic data type addition, reference data type addition, and the underlying operation rules, [] + {}, {} + []
asp日期函数(磁盘函数不正确怎么办)
POI特效 市场调研
serialization and deserialization
类和对象(3)
Drive board network cable directly connected to computer shared network configuration
物理防火墙是什么?有什么作用?
Servlet overview
[wechat applet] page navigation