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.

很显然是一个动态规划的问题,找到递归表达式就可以了,考虑到会有负负相乘的问题,所以应该维持一个最大的值以及一个最小的值,递归表达式如下所示:

 maxLocal[i + ] = max(max(maxLocal[i] * nums[i + ], minLocal[i] * nums[i + ]), nums[i + ]);
minLocal[i + ] = min(min(maxLocal[i] * nums[i + ], minLocal[i] * nums[i + ]), nums[i + ]);
maxGlobal[i + ] = max(maxGlobal[i], maxLocal[i + ]);

代码如下所示:

 class Solution {
public:
int maxProduct(vector<int>& nums) {
int sz = nums.size();
if (sz == )return ;
if (sz == )return nums[];
int maxP, minP, a, b, ret;
ret = maxP = minP = a = b = nums[];
for (int i = ; i < sz; ++i){
a = max(maxP * nums[i], minP * nums[i]);
b = min(maxP * nums[i], minP * nums[i]);
maxP = max(a, nums[i]);
minP = min(b, nums[i]);
ret = max(maxP, ret);
}
return ret;
} };

java版本的如下所示,注意使用两个中间变量的原因是防止maxVal先直接更新了之后又被minVal所误用:

 public class Solution {
public int maxProduct(int[] nums) {
int maxVal, minVal, ret, tmpMax, tmpMin;
maxVal = minVal = ret = tmpMax = tmpMin = nums[0];
for(int i = 1; i < nums.length; ++i){
tmpMax = Math.max(maxVal * nums[i], minVal * nums[i]);
tmpMin = Math.min(maxVal * nums[i], minVal * nums[i]);
maxVal = Math.max(tmpMax, nums[i]);
minVal = Math.min(tmpMin, nums[i]);
ret = Math.max(maxVal, ret);
}
return ret;
}
}

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

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

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

  2. C#解leetcode 152. Maximum Product Subarray

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

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

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

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

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

  5. LeetCode之Maximum Product Subarray

    1.(原文)问题描述 Find the contiguous subarray within an array (containing at least one number) which has t ...

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

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

  7. LeetCode 152. Maximum Product Subarray (最大乘积子数组)

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

  8. [leetCode][001] Maximum Product Subarray

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

  9. Java for LeetCode 152 Maximum Product Subarray

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

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

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

随机推荐

  1. SPL(Standard PHP Library 标准PHP类库)

    SplFileObject 读取大文件从第N行开始读: $line = 10; $splFileObj = new SplFileObject(__FILE__,'r'); $splFileObj-& ...

  2. linux安装tree命令

    安装 yum install -y tree 使用,比如显示/root的2层树结构 tree -L 2 /root 效果 /root ├── \033 ├── code │   └── hellowo ...

  3. nginx缓存原理

    一.HTTP字段理解 1.Expires: 该字段的http1.0时的规范,值为一个绝对时间的GMT格式的时间字符串,代表缓存资源的过期时间,在这个时点之前即命中缓存. 缺点:服务器返回的时间,可能与 ...

  4. MySQL ——索引原理与慢查询优化(Day45)

    阅读目录 一 介绍 二 索引的原理 三 索引的数据结构 三 MySQL索引管理 四 测试索引 五 正确使用索引 六 查询优化神器-explain 七 慢查询优化的基本步骤 八 慢日志管理 ====== ...

  5. ASYNCAPI

    https://www.asyncapi.com Introduction AsyncAPI provides a specification that allows you to define Me ...

  6. Kotlin学习记录3

    参考我的博客:http://www.isedwardtang.com/2017/09/04/kotlin-primer-3/

  7. Linux常用命令(6/26)——dd命令和split命令

    dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 以可选块长度复制文件,默认情况下从标准输入设备输出到标准输出设备.复制过程中,还可以对文件进行一些转换. dd命令可以指定block的 ...

  8. ijkplayer实现IMediaDataSource

    由于ijkplayer不能识别android.resource类型的资源在播放raw中的文件的时候用IjkMediaPlayer不能正常播放,实现IMediaDataSource为IjkMediaPl ...

  9. Apache 浏览器访问限制配置

    浏览器访问限制配置 user_agent收入的浏览器中,我们通过百度,谷歌很容易就可以查到相关的一些资料,方便了我们对知识的查找,但在某些特定情况下,我们并不希望有人可以通过某写搜索引擎直接访问到我们 ...

  10. 函数:生成1-n的随机数组,

    方法很笨,不过可行: #include <stdio.h> /** 功能:获取一个1-n的随机数数组,这些随机数都互不相同 ** 入参:n-表示最大随机数: *randArray -用于储 ...