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. Java: AutoCloseable接口

    K7 增加了一些新特性,其中报错AutoCloseable 等.新特性包括如下,下面主要说说AutoCloseable . 在JDK7 中只要实现了AutoCloseable 或Closeable 接 ...

  2. 下载安装ActiveMQ(消息队列)

    安装步骤: 第一步.安装jdk环境,因为ActiveMQ是使用java语言编写. 第二步.将下载好的activemq压缩包上传至Linux系统,进行解压. 第三步.进入解压后的bin/目录,进行启动a ...

  3. PowerBI开发 第五篇:关系的设计

    PowerBI 使用 内存的列式数据库 VertiPaq,用于对已发布的数据集进行数据压缩和快速处理,能够使PowerBI报表执行脱机访问,面向列的处理,高度优化对1:N关系的处理性能.PowerBI ...

  4. 设置文件opendilag、savedilog默认路径和文件类型

    dlgSave1.Filter:='文件.txt|*.txt;|word文件|*.doc,*.docx'; dlgSave1.InitialDir:=GetCurrentDir; //设置为当前路径 ...

  5. Mybatis第六篇【配置文件和映射文件再解读、占位符、主键生成与获取、Mapper代理】

    配置文件和映射文件再解读 映射文件 在mapper.xml文件中配置很多的sql语句,执行每个sql语句时,封装为MappedStatement对象,mapper.xml以statement为单位管理 ...

  6. 快速设计ComboBox下拉框

    传统软件项目开发时,需要每个控件一个一个的来设计,同时需要在页面功能中对每个控件的属性进行判定处理,尤其是页面风格布局样式需要花去一大半的时间,并且后续要想修改是非常麻烦繁琐,这样就导致设计完成一个功 ...

  7. 配置exVim开发环境

    exVim主页 http://exvim.github.io/ 使用该配置原因: 简单,组织各种优秀插件,安装包很小,各种操作很流畅 实用,对于项目来说,只需要多出一个xx.exvim文件,所有符号等 ...

  8. 第5章 不要让线程成为脱缰的野马(Keeping your Threads on Leash) ---干净的终止一个线程

    干净的终止一个线程  我曾经在第2章产生一个后台线程,用以输出一张屏幕外的 bitmap 图.我们必须解决的一个最复杂的问题就是,如果用户企图结束程序,而这张bitmap 图尚未完成,怎么办?第2章的 ...

  9. 【转】常用Maven插件

    我们都知道Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完成,例如编译源代码是由maven- compiler-plugin完成的.进一步说,每个任务对应 ...

  10. Eddy's爱好 hdu2204

    Eddy's爱好 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...