当前位置:网站首页>152. Product maximum subarray - dynamic programming
152. Product maximum subarray - dynamic programming
2022-07-25 23:32:00 【Mr Gao】
152. Product maximum subarray
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 .
Example 1:
Input : nums = [2,3,-2,4]
Output : 6
explain : Subarray [2,3] There is a maximum product 6.
Example 2:
Input : nums = [-2,0,-1]
Output : 0
explain : The result can't be 2, because [-2,-1] It's not a subarray .
This question is very interesting , The solution code is as follows :
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;
}
边栏推荐
- Ffmpeg first learning (only for coding)
- XxE & XML external entity injection utilization and bypass
- 【JUC】并发需要了解的关键字volatile
- Serialize common default values and column parameters
- 加拿大EE通道
- MES系统设备管理概述(下)
- Classes and objects (3)
- idea设置get、set模板解决boolean类型字段的命名问题
- Several commonly used traversal methods
- Classes and objects (2) (6 default member functions)
猜你喜欢

类和对象(3)

wordpress去掉网站发布时间

Source code of YY music wechat applet imitating Netease cloud music

【MUDUO】EventLoop事件循环

How does Navicat modify the language (Chinese or English)?

多模态——Deep Multi-Modal Sets

Learning exploration-3d rotation card

ratio学习之ratio_add,ratio_subtract,ratio_multiply,ratio_divide的使用

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

图的遍历-DFS,BFS(代码详解)
随机推荐
762. 二进制表示中质数个计算置位
Source code of YY music wechat applet imitating Netease cloud music
JS regular expression content:
initializer_list工具库学习
谷粒学苑P98踩坑 e.GlobalExceptionHandler : null
[wechat applet] page navigation
S4/HANA ME21N创建PO 输出控制消息按钮丢失解决方法(切换EDI 输出模式BRF+至NAST模式)
Why are there many snapshot tables in the BI system?
Which securities company should a novice choose to open an account? Is it safe?
Npm+ module loading mechanism
Computed and watch listening properties
【JUC】并发需要了解的关键字volatile
Classes and objects (3)
Inheritance (the child constructor inherits the attributes in the parent constructor)
PHP binary array is sorted by a field in it
TS interface
Thinkphp6 temporarily close the layout
Take away applet with main version of traffic / repair to add main access function of traffic
EasyExcel实用技巧
npm+模块加载机制