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. heat创建stack

    1.使用模板创建虚拟机 heat_template_version: 2018-09-04 description: Simple template to deploy a virtual machi ...

  2. day15模块内容

    1.生成器表达式 先说三元表达式如下 res = [i for i in range(10) if 1 > 5] 这样res就是一个列表6,7,8,9] 只要在这个基础上稍加调整,如下 方括号改 ...

  3. .resources文件转为可视化.resx文件

    ResGen.exe启动闪退.--方法不对 参考:http://www.opdown.com/soft/101205.html 在cmd中启动ResGen.exe. 打开cmd:输入 C:\Windo ...

  4. Android笔记:OptionsMenu

    使用菜单选项OptionsMenu,需要进行以下操作:(1)重写onCreateOptionsMenu方法: public boolean onCreateOptionsMenu(Menu menu) ...

  5. 不同CSS技术及其CSS性能

    OOCSS样式:一个主class,包含所有的共同规则,然后一个独特的规则使用其他class .box {padding:25px;border:1px solid #000;border-radius ...

  6. elasticsearch 不同集群数据同步

    采用快照方式 1.源集群采用NFS,注意权限 2.共享目录完成后,在所有ES服务器上挂载为同一目录 3.创建快照仓库 put _snapshot/my_backup{ "type" ...

  7. Centos + Maven + Jenkins

    下载 JDKwget --no-check-certificate --no-cookie --header "Cookie: oraclelicense=accept-secureback ...

  8. Android Studio生成签名安装包(Generate Signed APK)

    一 打开构建对话框. 二 创建新的密钥库(key store) 可以选择已创建的密钥库,也可以选择创建新的密钥库. 创建完成后,自动导入. 三 选择签名类型. 如果不选,会提示错误. 这里将新旧两种签 ...

  9. http://www.rabbitmq.com/documentation.html

    http://www.rabbitmq.com/documentation.html https://www.gitbook.com/book/geewu/rabbitmq-quick/details

  10. MVC 的那点小事

    两年未见 一切从头再来.我猜到了故事的开头,找工作一如我想象的那般艰难,但是结果却比我预期的要好很多. 第一次开始用MVC 框架,比我想象的要简单的多,就像同事跟我说的,这只是个框架. 言归正传,前两 ...