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. HDOJ 4747 Mex

    非常好的线段树题....此题必定会火..... Mex Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K ( ...

  2. VS上利用C#实现一个简单的串口程序记录

    一.背景 工作上需要利用串口往下位机写入数据,VC太老,正好借此机会来熟悉一直很想接触的VS之C#. 感谢Tony托尼哥的串口通信代码,感谢梦真的C#的技术支持. 二.正文 1.项目架构:(以我现有的 ...

  3. 解决升级WordPress及插件需输入FTP账号的问题

    当添加,删除,升级 WordPress 插件或者直接升级 WordPress 的时候,WordPress 总是提示让你输入 FTP 帐号信息,非些烦人. 我们可以在 wp-config.php 中定义 ...

  4. bootstrap实现弹出窗口

    bootstrap使用modal-dialog实现弹对话框. 一个对话框包含3部分: 对话框头部 modal-header 对话框内容体 modal-body 对话框底部 modal-footer 如 ...

  5. webpack 教程 那些事儿04-webpack项目实战分析

    这节主要讲解真正项目用用到的 webpack配置问题,项目实战篇 就像我们不会完全做一个项目,不用别人的轮子一样.这个配置我们借用 vue-cli 搭建的配置来研究,因为它已经足够优秀. 有了前面的基 ...

  6. android ListView嵌套GridView显示不全问题

    只需重写GridView即可:public class MyGridView extends GridView{ public MyGridView(android.content.Context c ...

  7. iOS开发——源代码管理——SVN

    一.源代码管理(svn)简介 01. 源代码管理工具概述 ======================================================================= ...

  8. Unity开发-你必须知道的优化建议

    转自:http://blog.csdn.net/leonwei/article/details/18042603 最近研究U3D开发,个人认为,精通一种新的技术,最快最好的方法就是看它的documen ...

  9. osx xcode 创建python项目

    http://stackoverflow.com/questions/5276967/python-in-xcode-7

  10. python 集合 -----直接用逗号连接的是元组,不是list

    t = 12345, 54321, 'hello!' >>> t[0] 12345 >>> t (12345, 54321, 'hello!') 元组:元组由逗号分 ...