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. 解决pydev报unsolved import的问题

    安装Flask_RESTful-0.2.11包后, 并在pydev 对应的 interpreter 重新刷新了System PYTHONPATH, 看见Lib\site-packages\flask_ ...

  2. Request 传值 遇到的中文乱码问题

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="xxxx.aspx.cs&quo ...

  3. 黄学长模拟day1 球的序列

    N个编号为1-n的球,每个球都有唯一的编号.这些球被排成两种序列,分别为A.B序列,现在需要重新寻找一个球的序列l,对于这个子序列l中任意的两个球,要求j,k(j<k),都要求满足lj在A中位置 ...

  4. C#2.0 特性

    泛型 迭代器 分布类 可空类型 匿名方法 命名空间别名限定符 静态类 外部程序程序集别名 属性访问器可访问性 委托中的协变和逆变 如何声明.实例化.使用委托 固定大小的缓冲区 友元程序集 内联警告控制 ...

  5. SQL 语句-partition by

    /****** ******/ 初始化数据 create table employee (empid int ,deptid int ,salary decimal(10,2)) insert int ...

  6. hdu.1226.超级密码(bfs)

    超级密码 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  7. 【PHP面向对象(OOP)编程入门教程】3.什么是面向对象编程呢?

    就不说他的概念,如果你想建立一个电脑教室,首先要有一个房间, 房间里面要有N台电脑,有N个桌子, N个椅子, 白板, 投影机等等,这些是什么,刚才咱们说了, 这就是对象,能看到的一个个的实体,可以说这 ...

  8. 中国天气预报数据API收集

      {"weatherinfo":{"city":"北京","cityid":"101010100" ...

  9. MySQL中快速复制数据表方法汇总

    本文将着重介绍两个MySQL命令的组合,它将以原有数据表为基础,创建相同结构和数据的新数据表. 这可以帮助你在开发过程中快速的复制表格作为测试数据,而不必冒险直接操作正在运行 的数据表. 示例如下: ...

  10. MongoDB的安全(五)

    MongoDB用户管理操作: MongoDB开启权限认证的方式有两种一种是auth形式,一种是keyfile形式 MongoDB创建用户: 1. 创建用户语法:在MongoDB2.6版本之后使用cre ...