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. HashMap、LinkedHashMap、ConcurrentHashMap、ArrayList、LinkedList 底层实现

    HashMap相关问题 1.你用过HashMap吗?什么是HashMap?你为什么用到它? 用过,HashMap是基于哈希表的Map接口的非同步实现,它允许null键和null值,且HashMap依托 ...

  2. 第八篇:Jmeter的分布式测试

    一: 由于Jmeter本身的瓶颈,当模拟数以千计的用户并发的时候,使用单台机器会有些力不从心,甚至还会引起Java内存溢出的错误,要解决这个问题,就要使用分布式测试,运行多台机器,也就是所谓的Agen ...

  3. 解题(StringJiaMi--字符串加密)

    题目描述 有一种技巧可以对数据进行加密,它使用一个单词作为它的密匙.下面是它的工作原理:首先,选择一个单词作为密匙,如TRAILBLAZERS.如果单词中包含有重复的字母,只保留第1个,其余几个丢弃. ...

  4. 766. Toeplitz Matrix

    A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now given ...

  5. 四 sys模块

    1 sys.argv 命令行参数List,第一个元素是程序本身路径 2 sys.exit(n) 退出程序,正常退出时exit(0) 3 sys.version 获取Python解释程序的版本信息 4 ...

  6. scrollIntoView 顶部与视图(容器)对齐

    调用方法为 element.scrollIntoView() 参数默认为true. 参数为true时调用该函数,页面(或容器)发生滚动,使element的顶部与视图(容器)顶部对齐: 参数为false ...

  7. python 自动补全

    一.查看python 环境变量 >>> import sys>>> sys.path 编写 tab.py import sys import atexit impo ...

  8. 学习Junit资料

    以下是找到的一些有用的学习资料,先收藏了 http://www.blogjava.net/jiangshachina/archive/2011/12/14/366289.html http://www ...

  9. f5创建VS

    1.1) 2) 3) 4) 5) 2.Availability 1)Pool 中的monitor保障服务高可用 2)Pool 失败机制一 Fallback Host 最后的host( 使用于HTTP ...

  10. VS中,添加完Web引用(WebServer引用/Web服务引用),写代码时引用不到

    VS中,添加完Web引用(WebServer引用/Web服务引用),写代码时引用不到 添加完之后要等一会儿 等一会儿 等一会儿 就有了