近期一直忙着写paper,非常久没做题,一下子把题目搞复杂了。。思路理清楚了非常easy,每次仅仅需更新2个值:当前子序列最大乘积和当前子序列的最小乘积。最大乘积被更新有三种可能:当前A[i]>0,乘曾经面最大的数(>0),得到新的最大乘积;当前A[i]<0,乘曾经面最小的数(<0),得到新的最大乘积;A[i]它自己>0,(A[i-1]==0。最小乘积同理。。

class Solution {
public: int Max(int a, int b, int c)
{
int max = -1 << 30; if (a >= b)
max = a;
else
max = b;
if (c > max)
max = c; return max;
} int Min(int a, int b, int c)
{
int min = 1 << 30; if (a <= b)
min = a;
else
min = b;
if (c < min)
min = c; return min;
} int maxProduct(int A[], int n) {
int maxPPrev=A[0], maxP;
int minPPrev = A[0], minP; int max = A[0]; for (int i = 1; i < n; i++)
{
maxP = Max(maxPPrev * A[i], minPPrev * A[i], A[i]);
minP = Min(maxPPrev * A[i], minPPrev * A[i], A[i]); maxPPrev = maxP;
minPPrev = minP; if (maxPPrev > max)
max = maxPPrev; if (minPPrev > max)
max = minPPrev;
} return max;
}
};

DP Leetcode - Maximum Product Subarray的更多相关文章

  1. LeetCode Maximum Product Subarray(枚举)

    LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...

  2. LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关

    Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...

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

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

  4. [LeetCode] Maximum Product Subarray 连续数列最大积

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

  5. Leetcode Maximum Product Subarray

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

  6. 152.[LeetCode] Maximum Product Subarray

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

  7. [leetcode]Maximum Product Subarray @ Python

    原题地址:https://oj.leetcode.com/problems/maximum-product-subarray/ 解题思路:主要需要考虑负负得正这种情况,比如之前的最小值是一个负数,再乘 ...

  8. LeetCode Maximum Product Subarray 解题报告

    LeetCode 新题又更新了.求:最大子数组乘积. https://oj.leetcode.com/problems/maximum-product-subarray/ 题目分析:求一个数组,连续子 ...

  9. LeetCode Maximum Product Subarray 最大子序列积

    题意:给一个size大于0的序列,求最大的连续子序列之积.(有正数,负数,0) 思路:正确分析这三种数.0把不同的可能为答案的子序列给隔开了,所以其实可以以0为分隔线将他们拆成多个序列来进行求积,这样 ...

随机推荐

  1. ASP.NET之Cookie(坑爹的Response.Cookies.Remove)

    原文:ASP.NET之Cookie(坑爹的Response.Cookies.Remove) 在web开发中Cookie是必不可少的 .NET自然也有一个强大的Cookie操作类,我们用起来也非常方便, ...

  2. mysql copy复制拷贝表数据及结构的几种方式(转)

    mysql拷贝表操作我们会常常用到,下面就为您详细介绍几种mysql拷贝表的方式,希望对您学习mysql拷贝表方面能够有所帮助.假如我们有以下这样一个表:id username password--- ...

  3. POJ 3450 Corporate Identity KMP解决问题的方法

    这个问题,需要一组字符串求最长公共子,其实灵活运用KMP高速寻求最长前缀. 请注意,意大利愿父亲:按照输出词典的顺序的规定. 另外要提醒的是:它也被用来KMP为了解决这个问题,但是很多人认为KMP使用 ...

  4. C# 6.0 (C# vNext) 的新功能:Expression Bodied Functions and Properties

    Expression Bodied Function 它可以用在: methods user-defined operators type conversions read-only properti ...

  5. java输入输出高速

    头文件: import java.io.*; 定义: BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); ...

  6. Linux下安装Oracle11g服务器(转)

    安装环境 Linux服务器:SuSe10 sp2 64位 Oracle服务器:Oracle11gR2 64位 系统要求 Linux安装Oracle系统要求 系统要求 说明 内存 必须高于1G的物理内存 ...

  7. SpringMVC+Spring+Hibernate的小样例

    Strusts2+Spring+Hibernate尽管是主流的WEB开发框架,可是SpringMVC有越来越多的人使用了.确实也很好用.用得爽! 这里实现了一个SpringMVC+Spring+Hib ...

  8. 应对黑客攻击SQL SERVER数据库中的一个案例

    最近发现挂在网上server不知怎的,重新启动,那server现在主要是开始IIS服务,SQL SERVER 服务. 远程登录.发现系统响应十分缓慢.一个明显的停滞感,打开任务管理器,CPU在基本用法 ...

  9. 【Linux探索之旅】第二部分第四课:文件操纵,鼓掌之中

    内容简介 1.第二部分第四课:文件操纵,鼓掌之中 2.第二部分第五课预告:用户和权限 文件操纵,鼓掌之中 既然上一课我们学习了Linux中的文件组织方式,那么现在就该是玩弄,啊不,是操纵它们的时候了. ...

  10. DSR on Openstack POC

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWFvbGlwaW5nNDU1bWxwNDU1/font/5a6L5L2T/fontsize/400/fil ...