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

For example, given the array [2,3,-2,4],
the contiguous subarray [2,3] has the largest product = 6.

找数字连续最大乘积子序列。

思路:这个麻烦在有负数和0,我的方法,如果有0,一切都设为初始值。

对于两个0之间的数若有奇数个负数,那则有两种情况,第一种是不要第一个负数和之前的值,第二种是不要最后一个负数和之后的值,用negtiveFront和negtiveBack表示。没有负数就是不要第一个负数和之前的值的情况。

int maxProduct(int A[], int n) {
if(n == )
return ; int MaxAns = A[];
int negtiveFront = (A[] == ) ? : A[];
int negtiveBack = (A[] < ) ? : ; for(int i = ; i < n; i++)
{
if(A[i] == )
{
MaxAns = (MaxAns > ) ? MaxAns : ;
negtiveFront = ;
negtiveBack = ;
}
else if(A[i] < )
{
negtiveFront *= A[i];
MaxAns = max(negtiveFront, MaxAns);
if(negtiveBack == )
{
negtiveBack = ;
}
else
{
negtiveBack *= A[i];
MaxAns = max(negtiveBack, MaxAns);
}
}
else
{
negtiveFront *= A[i];
negtiveBack *= A[i];
MaxAns = max(negtiveFront, MaxAns);
if(negtiveBack > )
{
MaxAns = max(negtiveBack, MaxAns);
} }
} return MaxAns;
}

答案的思路:同时维护包括当前数字A[k]的最大值f(k)和最小值g(k)

f(k) = max( f(k-1) * A[k], A[k], g(k-1) * A[k] )
g(k) = min( g(k-1) * A[k], A[k], f(k-1) * A[k] )

再用一个变量Ans存储所有f(k)中最大的数字就可以了

int maxProduct2(int A[], int n) {
if(n == )
return ; int MaxAns = A[]; //包括当前A【i】的连续最大乘积
int MinAns = A[]; //包括当前A【i】的连续最小乘积
int MaxSoFar = A[]; //整个数组的最大乘积 for(int i = ; i < n; i++)
{
int MaxAnsTmp = MaxAns;
int MinAnsTmp = MinAns;
MaxAns = max(MaxAnsTmp * A[i], max(MinAnsTmp * A[i], A[i]));
MinAns = min(MinAnsTmp * A[i], min(MaxAnsTmp * A[i], A[i]));
MaxSoFar = max(MaxSoFar, MaxAns); } return MaxSoFar;
}

【leetcode】 Unique Binary Search Trees (middle)☆的更多相关文章

  1. 【leetcode】Unique Binary Search Trees

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  2. 【leetcode】Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  3. 【LeetCode】Unique Binary Search Trees II 异构二叉查找树II

    本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html 原题: Given n, generate all struc ...

  4. 【leetcode】 Unique Binary Search Trees II (middle)☆

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  5. 【leetcode】Unique Binary Search Trees (#96)

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  6. 【题解】【BST】【Leetcode】Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  7. 【Leetcode】【Medium】Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  8. 【Leetcode】【Medium】Unique Binary Search Trees II

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  9. 【Leetcod】Unique Binary Search Trees II

    给定结点数n,结点值为1,2,...,n,求由这些结点可以构成的所有二叉查找树. Given n, generate all structurally unique BST's (binary sea ...

随机推荐

  1. 大熊君大话NodeJS之------Buffer模块

    一,开篇分析 所谓缓冲区Buffer,就是 "临时存贮区" 的意思,是暂时存放输入输出数据的一段内存. JS语言自身只有字符串数据类型,没有二进制数据类型,因此NodeJS提供了一 ...

  2. Redhat系统网络配置

    1.RedHat系统的网络配置文件/etc/sysconfig/network-scirpts/ifcfg-<interface-name>文件 DEVICE=eth0           ...

  3. 繁华模拟赛day8 牛栏

    /* 标称并没有用到题解中提到的那种奇妙的性质,我们可以证明,正常从1开始走的话,需要T次,如何使这个次数减小?题解中提到一个办法,有一步小于n/t,我们考虑这一步,如果把它匀到左右两步中,则可以减小 ...

  4. Entity Framework浅析

    1.Entity Framework简介 http://www.cnblogs.com/aehyok/p/3315991.html 2.Entity Framework DBFirst尝试http:/ ...

  5. Codeforces Round #270 1003

    Codeforces Round #270 1003 C. Design Tutorial: Make It Nondeterministic time limit per test 2 second ...

  6. 【PHP面向对象(OOP)编程入门教程】13.访问类型(public,protected,private)

    类型的访问修饰符允许开发人员对类成员的访问进行限制,这是PHP5的新特性,但却是OOP语言的一个好的特性.而且大多数OOP语言都已支持此特性.PHP5支持如下3种访问修饰符: public (公有的. ...

  7. Swift---TextView用法

    1.TextView放在一个NSScrollView中,作为documentView存在. @IBOutlet weak var txtScrollView: NSScrollView! 2.在Tex ...

  8. setValuesForKeysWithDictionary forUndefinedKey

    http://my.oschina.net/u/2407613/blog/524879 -(void)setValue:(id)value forUndefinedKey:(NSString *)ke ...

  9. JS的构造函数

    //构造函数  //使自己的对象多次复制,同时实例根据设置的访问等级可以访问其内部的属性和方法  //当对象被实例化后,构造函数会立即执行它所包含的任何代码  function myObject(ms ...

  10. iOS开发——UI进阶篇(六)键盘处理

    一.键盘通知我们经常需要在键盘弹出或者隐藏的时候做一些特定的操作,因此需要监听键盘的状态 键盘状态改变的时候,系统会发出一些特定的通知UIKeyboardWillShowNotification // ...