Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product.

Example 1:

Input: [2,3,-2,4]
Output: 6
Explanation: [2,3] has the largest product 6.

Example 2:

Input: [-2,0,-1]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.

Idea 1. How can we extend a solution from nums[0..i] to nums[0..i+1]? what shall we do for a newly added element nums[i+1]. Similar to Maximum Subarray LT53, assume we know the largest product for array nums[0..i],

if nums[i+1] >= 0, maxProduct(i+1) = Math.max(maxProduct(i) * nums[i+1], nums[i+1])

if nums[i+1] < 0, to get the max product ending at (i+1), we also need to know the min element ending at i which could be negative, maxProduct(i+1) = Math.max(minProduct(i) * nums[i+1], nums[i+1]).

For any element at i+1, maxProduct(i+1) = Math.max( nums[i+1], minProduct(i) * nums[i+1], maxProduct(i) * nums[i+1] )

Time complexity: O(n)

Space complexity: O(1)

 class Solution {
public int maxProduct(int[] nums) {
int minHere = 1, maxHere = 1, result = Integer.MIN_VALUE; for(int num: nums) {
int res1 = minHere * num;
int res2 = maxHere * num;
if(res1 < res2) {
minHere = Math.min(res1, num);
maxHere = Math.max(res2, num);
}
else {
minHere = Math.min(res2, num);
maxHere = Math.max(res1, num);
}
result = Math.max(result, maxHere);
}
return result;
}
}
 class Solution {
public int maxProduct(int[] nums) {
int minHere = 1, maxHere = 1, result = Integer.MIN_VALUE; for(int num: nums) {
int res1 = minHere * num;
int res2 = maxHere * num; minHere = Math.min(Math.min(res1, res2), num);
maxHere = Math.max(Math.max(res1, res2), num); result = Math.max(result, maxHere);
}
return result;
}
}

Maximum Product Subarray LT152的更多相关文章

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

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

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

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

  3. LeetCode Maximum Product Subarray(枚举)

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

  4. LeetCode_Maximum Subarray | Maximum Product Subarray

    Maximum Subarray 一.题目描写叙述 就是求一个数组的最大子序列 二.思路及代码 首先我们想到暴力破解 public class Solution { public int maxSub ...

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

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

  6. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

  7. 【刷题-LeetCode】152 Maximum Product Subarray

    Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...

  8. 152. Maximum Product Subarray - LeetCode

    Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...

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

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

随机推荐

  1. SpringMVC Controller 单例 多例

    对于SpringMVC 的Controller单例还是多例.下面举例说明:第一次:类是多例,类里包含一个普通属性,一个静态属性 结果:普通属性:0.............静态属性:0 普通属性:0. ...

  2. SSM的例子-参考

    ssm的例子:http://blog.csdn.net/double030/article/details/63683613

  3. 牛客练习赛43-F(简单容斥)

    题目链接:https://ac.nowcoder.com/acm/contest/548/F 题意:简化题意之后就是求[1,n]中不能被[2,m]中的数整除的数的个数. 思路:简单容斥题,求[1,n] ...

  4. 187. Repeated DNA Sequences (String; Bit)

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  5. Reportviewer中的函数使用——打印当前日期并格式化

    如2017-10-23 12:20:20 通过DateTime.Now.ToString("yyMMddHHmmss")变为 20171023122020字符串

  6. 安装tensorflow ubuntu18.04

    1.首先安装环境是ubuntu18.04. $sudo apt-get install python-pip python-dev python-virtualenv2.安装virtualenv虚拟环 ...

  7. 大话listview之设置item监听器无效大坑之一:item设了属性clickable

    今天一个listview设置item监听器居然没有作用: 看了半天,怀疑是item设置了这个属性, 于是删了,果然就可以了. 大坑 ...

  8. jsplumb流程器使用2

    jsplumb默认注册在浏览器的窗口,为整个页面提供静态实例 1. 单独实例化的方法: var firstInstance = jsPlumb.getInstance(); 内部传入可定义对象   全 ...

  9. No appenders could be found for logger

    在运行代码时,出现下面的错误, log4j:WARN No appenders could be found for logger (genericTest.GenericTest). log4j:W ...

  10. YII2开启路由配置后,新加的模块无法访问

    最近使用YII2,自定义创建了一个自定义模块users,位置为app\modules\users. 'modules' => [ 'users' => [ 'class' => 'a ...