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. JavaScript总体的介绍【JavaScript介绍、定义函数方式、对象类型、变量类型】

    什么是JavaScript? 我们可以从几个方面去说JavaScript是什么: 基于对象 javaScript中内置了许多对象供我们使用[String.Date.Array]等等 javaScrip ...

  2. 如何使用fiddler抓取https请求(PC和移动端)

    最近做一个抓取移动端app接口,并执行评论,收藏的接口功能测试.怎么搞/(ㄒoㄒ)/~~ 按照老思路试一试,第一步还是要用fiddler来帮忙获取接口信息! 一.基本的抓取http请求设置: 1.cm ...

  3. stl 和并查集应用

    抱歉这么久才写出一篇文章,最近进度有点慢.这么慢是有原因的,我在想如何改进能让大家看系列文章的时候更方便一些,现在这个问题有了答案,在以后的推送中,我将尽量把例题和相关知识点在同一天推出,其次在代码分 ...

  4. border-radius:50%和100%究竟有什么区别

    之前写css圆形时总是直接设置border-radius为50%.后来看某css动画网站时发现作者都是用的100%.遂去了解了一下2者的差别. border-radius的值是百分比的话,就相当于盒子 ...

  5. SQLServer总结

    基础 nvarchar 和 varchar等的区别 1.nvarchar多了一个N,n表示使用的unicode编码,不用N开头的是用的utf-8编码. 2.所以中文在varchar中占两个字符长度,在 ...

  6. SSH端口转发详解及实例

    一.SSH端口转发简介 SSH会自动加密和解密所有SSH客户端与服务端之间的网络数据.但是,SSH还能够将其他TCP端口的网络数据通SSH链接来转发,并且自动提供了相应的加密及解密服务.这一过程也被叫 ...

  7. Hex to Int 【十六进制转十进制】

    long HexToInt(char *msgline){    long strlength,chvalue,tvalue;    WORD i;    chvalue=0;    strlengt ...

  8. SpringBoot文档翻译系列——26.日志logging

    原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/7613854.html 这是SpringBoot的日志内容 26 日志 Spring使用Co ...

  9. SGU180(树状数组,逆序对,离散)

    Inversions time limit per test: 0.25 sec. memory limit per test: 4096 KB input: standard output: sta ...

  10. Ubuntu 16.04 源码编译安装PHP7

    一.下载PHP7的最新版源码 php7.0.9  下载地址 http://php.net/get/php-7.0.9.tar.gz/from/a/mirror 二.解压 tar -zxf /tmp/p ...