当前位置:网站首页>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;
}
边栏推荐
- 2021-09-30
- Idea sets get and set templates to solve the naming problem of boolean type fields
- Mongodb query and projection operators
- 2022牛客多校第二场
- Anaconda installation tutorial environment variables (how to configure environment variables)
- The difference between MySQL clustered index and non clustered index
- WebMvcConfigurationSupport
- anaconda安装教程环境变量(如何配置环境变量)
- Serialize common default values and column parameters
- About priority queues
猜你喜欢

电商RPA,大促轻松上阵的法宝

Wamp MySQL empty password

Analysis of direction finding error of multi baseline interferometer system

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

Dynamic memory management

【代码案例】博客页面设计(附完整源码)

Take root downward, grow upward, and explore the "root" power of Huawei cloud AI

类和对象(2)(6个默认成员函数)

Very simple vsplayaudio online music player plug-in

General paging function
随机推荐
POI特效 市场调研
学习探索-波浪
Mongodb update operator (modifier)
Check code generation
[QNX hypervisor 2.2 user manual]9.7 generate
Solve the problem phpstudy failed to import the database
TS union type
r语言绘图参数(R语言plot画图)
Yii2 behavior usage and its calling method
BI 系统中为什么会有很多快照表?
PHP JSON variable array problem
Take away applet with main version of traffic / repair to add main access function of traffic
Vscode shortcut key: collapse and expand code
PHP wechat scan code, follow official account and authorize login source code
POI special effects Market Research
Wechat official account, wechat payment development
Analysis of direction finding error of multi baseline interferometer system
@Import
Unity uses macros
物理防火墙是什么?有什么作用?