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的更多相关文章

  1. 【刷题-LeetCode】152 Maximum Product Subarray

    Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...

  2. 【LeetCode】152. Maximum Product Subarray 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双重循环 动态规划 参考资料 日期 题目地址:htt ...

  3. LeetCode OJ 152. Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  4. 【LeetCode】152. Maximum Product Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  5. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

  6. 152. Maximum Product Subarray - LeetCode

    Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...

  7. 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray

    题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...

  8. [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  9. LeetCode 152. Maximum Product Subarray (最大乘积子数组)

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

随机推荐

  1. 如何用js往html页面拼接一个div包括div的各种常用属性

    $("#div").append("<table><tr align='center'>" +"<td >&quo ...

  2. 关于使用CELERY的一点心得

    使用也有大半年了.稳定性没话说啊. 但有一个坑,是我以前没注意的,记录下来. 就是本来一个任务是可以异步并行执行的..但如何需要CELERY的执行结果来作判断的话,就会变得异步串行的. 这要值得注意. ...

  3. BZOJ(2) 1041: [HAOI2008]圆上的整点

    1041: [HAOI2008]圆上的整点 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4966  Solved: 2258[Submit][Sta ...

  4. python_swift_project_middleware

    1. 写openstack swift的middleware 首先要确定swift用的是哪个middleware文件. 比如healthcheck这个中间件,在机器上有很多同名文件如下, 这两行可能是 ...

  5. Windows 由于无法验证发布者,windows阻止控件安装怎么办

    1 打开Internet选项   2 下载未签名的ACTIVEX控件-设为启动    

  6. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第12章节--SP 2013中远程Event Receivers

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第12章节--SP 2013中远程Event Receivers         本章中,你讲学到: 了解远程evernt ...

  7. Caffe——清晰高效的深度学习(Deep Learning)框架

    Caffe(http://caffe.berkeleyvision.org/)是一个清晰而高效的深度学习框架,其作者是博士毕业于UC Berkeley的贾扬清(http://daggerfs.com/ ...

  8. 适用于app.config与web.config的ConfigUtil读写工具类 基于MongoDb官方C#驱动封装MongoDbCsharpHelper类(CRUD类) 基于ASP.NET WEB API实现分布式数据访问中间层(提供对数据库的CRUD) C# 实现AOP 的几种常见方式

    适用于app.config与web.config的ConfigUtil读写工具类   之前文章:<两种读写配置文件的方案(app.config与web.config通用)>,现在重新整理一 ...

  9. C++简单版BitSet求解大量数据是否存在莫个数

    #include <iostream> using namespace std; template<int N> class BitSet { public: BitSet() ...

  10. 解决myeclipse中struts2 bug问题包的替换问题

    由于struts2的bug问题,手工替换还是比較麻烦.但即便是最新的myeclipse2014也没有替换最新的struts2包,研究了一天,最终找到了解决的方法.下面就解决方法与大家分享一下. 1.在 ...