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.


题目标签:Array, Dynamic Programming

  题目给了我们一个nums array,让我们从中找到一个subarray, 它的乘积是最大的,返回乘积值。

  这道题目的难点在于,有0 和有 负数, 遇到0的话,就等于断点了,要重新开始记录新一段的subarray。遇到负数的话,如果是偶数的负数,那么依然可以保留,如果不是,那么也要重新开始记录。所以这道题目我们需要三个变量,来不断更新我们的subarray 的乘积。

  遍历nums array, max - 记录最大的subarray 乘积 从0 到 i。

           min  - 记录最小的subarray 乘积 从0 到 i,这里是需要到i, 在 i 前面的任何小段都不需要,为什么要记录最小的呢,因为有负数,要把最小的负值记录下来,当遇到新的负数,在可以配对成偶数的负数的情况下,把负数也利用进去。

           maxAns - 记录array 中 任意的最大乘积的 subarray 的值。

Java Solution:

Runtime beats 42.46%

完成日期:08/28/2017

关键词:Array, Dynamic Programming

关键点:保持记录从0 到 i 的最大和最小subarray 的乘积值

 class Solution
{
public int maxProduct(int[] nums)
{
if(nums.length == 0)
return 0; // save first number into max, min & maxAns
int max = nums[0];
int min = nums[0];
int maxAns = nums[0]; /* iterate rest number
* for each number, remember the max and min value for the previous product (0 ~ i)
*/
for(int i=1; i<nums.length; i++)
{
int tmp_max = max;
int tmp_min = min; // remember the max product subarray from 0 to i
max = Math.max(Math.max(nums[i], tmp_max * nums[i]), tmp_min * nums[i]);
/* remember the min product subarray from 0 to i
* min product subarray can only be from somewhere to i NOT somewhere to j that is before i
* because each time max use min and if min is not consecutive to current i, it is meaningless
*/
min = Math.min(Math.min(nums[i], tmp_max * nums[i]), tmp_min * nums[i]); // update the maxAns
maxAns = Math.max(max, maxAns);
} return maxAns;
}
}

参考资料:

http://www.cnblogs.com/grandyang/p/4028713.html

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 152. Maximum Product Subarray (最大乘积子数组)的更多相关文章

  1. 152. Maximum Product Subarray最大乘积子数组/是否连续

    [抄题]: Given an integer array nums, find the contiguous subarray within an array (containing at least ...

  2. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

  3. [leetcode]152. Maximum Product Subarray最大乘积子数组

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

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

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

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

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

  6. C#解leetcode 152. Maximum Product Subarray

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

  7. Java for LeetCode 152 Maximum Product Subarray

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

  8. leetcode 152. Maximum Product Subarray --------- java

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

  9. Leetcode#152 Maximum Product Subarray

    原题地址 简单动态规划,跟最大子串和类似. 一维状态空间可以经过压缩变成常数空间. 代码: int maxProduct(int A[], int n) { ) ; ]; ]; ]; ; i > ...

随机推荐

  1. sqlserver2012安装过程

    第三次安装sqlserver2012 记录下安装过程,受益的人多多指教. 一.以我的系统64位为例,所以先准备安装包,从官网 https://www.microsoft.com/zh-CN/downl ...

  2. 【机器学习实战】Machine Learning in Action 代码 视频 项目案例

    MachineLearning 欢迎任何人参与和完善:一个人可以走的很快,但是一群人却可以走的更远 Machine Learning in Action (机器学习实战) | ApacheCN(apa ...

  3. CSS公用

    *{font-size: 100px;} body,span,h1,h2,h3,h4,h5,h6,li,ul,p,em,strong,ol,form,pre,input,article,header, ...

  4. String类的构造方法(2)

    写了常见的几个而已. 1:new 一个String类的时候系统会自动传一个空构造 public String(); 注意: 当对象初始化是 null时 和 对象是 "" 时,两者是 ...

  5. Python dict operation introduce

    字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示: d = ...

  6. css常见布局方式

    CSS常见布局方式 以下总结一下CSS中常见的布局方式.本人才疏学浅,如有错误,请留言指出. 如需转载,请注明出处:CSS常见布局方式 目录: 使用BFC隐藏属性 float + margin abs ...

  7. Day4 闭包、装饰器decorator、迭代器与生成器、面向过程编程、三元表达式、列表解析与生成器表达式、序列化与反序列化

    一.装饰器 一.装饰器的知识储备 1.可变长参数  :*args和**kwargs def index(name,age): print(name,age) def wrapper(*args,**k ...

  8. Centos7 创建本地 docker 仓库极其遇到的问题

    环境安装: VirtualBox 安装 Centos7 安装 docker 1. 配置私有仓库和客户端地址 私有仓库:192.168.1.104 客户端:192.168.1.103 通过 Centos ...

  9. C# 7 局部函数剖析

    局部函数是C# 7中的一个新功能,允许在一个函数中定义另一个函数. 何时使用局部函数? 局部函数的主要功能与匿名方法非常相似:在某些情况下,创建一个命名函数在读者的认知负担方面代价太大.有时,函数本身 ...

  10. Windows+Apache2.4.10+PHP7.0+MySQL5.6.21安装

    一.安装包下载 apache2.4.10 http://www.apachelounge.com/download/win64/ PHP7.0.7 http://windows.php.net/dow ...