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.

mlgb的,今天遇到这个题搞了半天。记录下。

首先解放可以使用穷举。通过二维数组product[i][j]记录从i到j的乘积,然后遍历得到数组中最大元素,就是答案。

public int maxProduct(int[] A) {
int[][] product = new int[A.length][A.length];
int max = A[0];
for(int i = 0; i < A.length; i++) {
product[i][i] = A[i];
for (int j = i + 1; j < A.length; j++) {
product[i][j] = product[i][j - 1] * A[j];
max = Math.max(max, product[i][j]);
}
max = Math.max(max, A[i]);
}
return max;
}

简单明了。但是当然是超时。

然后撸主想了很久,期间还看了部电影。

假设第i元素是个正数,那么我们要计算当前i个元素的最大乘积p[i],就要 知道前面i-1的最大乘积p[i-1],p[i] = p[i - 1] * A[i];

假设第i元素是个负数,那么我们要计算当前i个元素的最大乘积p[i],就要知道前面i-1的最小乘积(负数)n[i-1], p[i] = n[i - 1] * A[i];

p[i]表示以第i元素结尾,能取到的最大乘积;n[i]表示以第i元素结尾,能取到的负乘积。如果i元素是0的花,一切归0,p[i] = n[i] = 0.

public int maxProduct2(int[] A) {
if (A.length == 1) {
return A[0];
}
int[] p = new int[A.length];
int[] n = new int[A.length];
int max = Integer.MIN_VALUE;
for (int i = 0; i < A.length; i++) {
if (A[i] > 0) {
p[i] = (i > 0 && p[i - 1] > 0) ? p[i - 1] * A[i] : A[i];
n[i] = (i > 0 && n[i - 1] < 0) ? n[i - 1] * A[i] : 0;
} else if (A[i] < 0) {
p[i] = (i > 0 && n[i - 1] < 0) ? n[i - 1] * A[i] : 0;
n[i] = (i > 0 && p[i - 1] > 0) ? p[i - 1] * A[i] : A[i];
} else {
max = Math.max(max, 0);
} if (p[i] > 0) {
max = Math.max(max, p[i]);
} else if (n[i] < 0) {
max = Math.max(max, n[i]);
}
}
return max;
}

因为遇到0的时候,其实是新的起点。

上面的代码可以简化为使用常量空间的下面的可读性较差的代码。

public int maxProduct(int[] A) {
if (A.length == 1) {
return A[0];
}
int p = 0;
int n = 0;
int max = Integer.MIN_VALUE;
for (int i = 0; i < A.length; i++) {
int np = p > 0 ? p * A[i] : A[i];
int nn = n < 0 ? n * A[i] : 0;
if (A[i] > 0) {
p = np;
n = nn;
} else if (A[i] < 0) {
p = nn;
n = np;
} else {
p = 0;
n = 0;
max = Math.max(max, 0);
continue;
}
if (p > 0) {
max = Math.max(p, max);
} else if (n < 0) {
max = Math.max(n, max);
}
}
return max;
}

LeetCode 笔记26 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

    This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...

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

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

  4. LeetCode OJ 152. Maximum Product Subarray

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

  5. 【LeetCode】152. Maximum Product Subarray

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

  6. LeetCode OJ:Maximum Product Subarray(子数组最大乘积)

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

  7. LeetCode Maximum Product Subarray(枚举)

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

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

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

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

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

随机推荐

  1. Java基础--常用正则匹配符号(必背,必须背,死都要背)

    1.字母:匹配单个字母 (1)A:表示匹配字母A: (2)\\:匹配转义字符“\”: (3)\t:匹配转义字符“\t”: (4)\n:匹配转义字符“\n”: 2.一组字符:任意匹配里面的一个单个字符: ...

  2. 傅里叶:有关FFT,DFT与蝴蝶操作(转 重要!!!!重要!!!!真的很重要!!!!)

    转载地址:http://blog.renren.com/share/408963653/15068964503(作者 :  徐可扬) 有没有!!! 其实我感觉这个学期算法最难最搞不懂的绝对不是动态规划 ...

  3. cocos2d-x图层以及显示对象的基本使用

    LogoNode: #ifndef LogoNode_hpp #define LogoNode_hpp #include <stdio.h> #include "cocos2d. ...

  4. 2013年 蓝桥杯预赛 java 本科A 题目

    1.标题: 世纪末的星期 曾有邪教称1999年12月31日是世界末日.当然该谣言已经不攻自破. 还有人称今后的某个世纪末的12月31日,如果是星期一则会.... 有趣的是,任何一个世纪末的年份的12月 ...

  5. 烂泥:KVM虚拟机windows系统增加硬盘

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 前一篇文章介绍了有关linux系统添加硬盘的方法,这次我们来介绍有关windows系统添加的相关步骤. 其实linux和windows添加的硬盘的方法都 ...

  6. python django 与数据库的交互

    下载没有任何问题的mysqdb http://www.codegood.com/archives/4 1创建一个新的app. python manage.py startapp books 2 激活a ...

  7. 如何去设计一个自适应的网页设计或HTMl5

    如何去设计一个自适应的网页设计或HTMl5 如今移动互联网随着3G的普及,越来越火爆,更多需求跟随而来!APP应用市场和APP应用数量成倍成倍的增长!从而给移动互联网带来新的挑战! 移动设备正超过桌面 ...

  8. HFSS 边界条件

    Ansoft HFSS求解就是对微分形式的麦克斯韦方程采取有限元方法进行数值求解,在场矢量和导数是都单值.有界而且沿空间连续分布的假设下,这些方程才可以使用.在边界和场 源处,场是不连续的,场的导数变 ...

  9. fiddler对手机进行抓包

    1.安装fiddler web debugger 2.Tools -->telerik fiddler options-->connections,勾选allow remote compu ...

  10. UVA-10269 (floyd+dijkstra)

    题意: 现在有A个村庄,B个城堡,现在要从1到A+B,有M条路,魔法鞋最多能用K次,每次的长度不超过L,且起点和终点一定是村庄和城堡,而且每次使用魔法鞋不能穿过城堡,问最短时间是多少; 思路: 先用F ...