[LeetCode]152. Maximum Product Subarray
This a task that asks u to compute the maximum product from a continue subarray. However, you need to watch
out the values' type contains positive, negative, zero.
I solved it using dynamic process in which there are two arrays to achieve the goal.
maxPro[i] : record the maximum product using nums[i] at end.
minPro[i] : record the minimum product using nums[i] at end.
Causing the neg and pos value types, we always can find the maximum product using the formula as below(maximum or minimum recording array):
- maxPro[i] = max3(nums[i], nums[i] * maxPro[i - 1], nums[i] * minPro[i - 1]);
- minPro[i] = min3(nums[i], nums[i] * maxPro[i - 1], nums[i] * minPro[i - 1]);
class Solution {
public:
int max3(int a, int b, int c){
return a > b? max(a, c): max(b, c);
}
int min3(int a, int b, int c){
return a < b? min(a, c): min(b, c);
}
int maxProduct(vector<int>& nums) {
vector<int>maxPro(nums.size(), );
vector<int>minPro(nums.size(), );
int ans = nums[];
for(int i = ; i < nums.size(); i ++){
if(i == ){
maxPro[] = nums[];
minPro[] = nums[];
}else{
maxPro[i] = max3(nums[i], nums[i] * maxPro[i - ], nums[i] * minPro[i - ]);
minPro[i] = min3(nums[i], nums[i] * maxPro[i - ], nums[i] * minPro[i - ]);
}
if(ans < maxPro[i]) ans = maxPro[i];
}
return ans;
}
};
[LeetCode]152. Maximum Product Subarray的更多相关文章
- 【刷题-LeetCode】152 Maximum Product Subarray
Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...
- 【LeetCode】152. Maximum Product Subarray 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双重循环 动态规划 参考资料 日期 题目地址:htt ...
- LeetCode OJ 152. Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【LeetCode】152. Maximum Product Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
- 152. Maximum Product Subarray - LeetCode
Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- LeetCode 152. Maximum Product Subarray (最大乘积子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
随机推荐
- Balanced Numbers(数位dp)
Description Balanced numbers have been used by mathematicians for centuries. A positive integer is c ...
- BZOJ 4006 Luogu P3264 [JLOI2015]管道连接 (斯坦纳树、状压DP)
题目链接: (bzoj)https://www.lydsy.com/JudgeOnline/problem.php?id=4006 (luogu)https://www.luogu.org/probl ...
- [K/3Cloud]如何解决kdpkg无法部署到业务站点的问题
自从下载了sp1后,就迫不急待的试用下,看看反馈的几个关键bug是否修复,可惜sp1安装后发现业务站点下的组件一个都没有被更新,这指定是有问题了,这真是让哥百思不得其解,真后悔在研发时没仔细研究下部署 ...
- 【Tomcat】tomcat配置多域名和虚拟路径
当我们用浏览器在访问网页的时候,如访问www.baidu.com,一般都认为会在DNS服务器上找这个域名对应的IP,然后向这个IP发送请求 并响应,其实在DNS服务器解析之前,本机会先在你的系统配置文 ...
- 洛谷——P1720 月落乌啼算钱
题目背景 (本道题目木有以藏歌曲……不用猜了……) <爱与愁的故事第一弹·heartache>最终章. 吃完pizza,月落乌啼知道超出自己的预算了.为了不在爱与愁大神面前献丑,只好还是硬 ...
- 几道splay
hdu 1890 题意:每次将第i位到第i小数字所在的位置之间的位置翻转,每次输出第i小数字所在的位置 分析: 简单的splay处理区间翻转问题 有三点需要注意: 1.区间是1~n+2 2.此题里的查 ...
- 模拟赛 Problem 1 高级打字机(type.cpp/c/pas)
Problem 1 高级打字机(type.cpp/c/pas) [题目描述] 早苗入手了最新的高级打字机.最新款自然有着与以往不同的功能,那就是它具备撤销功能,厉害吧. 请为这种高级打字机设计一个程序 ...
- Performance Tunning - OCP
This artical is forcused on Oracle 11g Release 2. It is an summary from the OCP documentation. The ...
- ubuntu16.04 卸载 php7并安装php5.6记录
ubuntu16.04版本从默认源安装的php版本为7.x版本,我们都知道php7.0已经舍弃了很多旧版本的函数等内容,这对旧系统来说是致命的,那么,我们就有了安装旧版php的需求,而同一主机安装两个 ...
- CI 日志类
开发ci的过程中,使用log能直观的看出代码运行到哪,还可设置代码查看数据接口的发送情况.日志类: <?php defined('BASEPATH') OR exit('No direct sc ...