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. 用BlazeMeter录制JMeter(三十五)测试脚本(转载)

    转载自 http://www.cnblogs.com/yangxia-test 工具: 1,JMeter 2,Chrome 3,BlazeMeter 4,SwitchyOmega(如果需要代理) 步骤 ...

  2. JMeter学习(十八)JMeter处理Cookie与Session(转载)

    转载自 http://www.cnblogs.com/yangxia-test 有些网站保存信息是使用Cookie,有些则是使用Session.对于这两种方式,JMeter都给予一定的支持. 1.Co ...

  3. Python基础之逻辑运算

    逻辑运算 概念: 优先级() > not > and > or print(2 > 1 and 1 < 4 or 2 < 3 and 9 > 6 or 2 & ...

  4. 使用scaleBitmap类缩放和拉伸

    使用scaleBitmap类缩放和拉伸 位图,画架,图形,小贴士我们一直在寻找在createJS中优化.简化或创建更好工作流的方法,scaleBitmap就是一个很好的例子.使用旧的flash方法,在 ...

  5. Linux系统一本通(实用篇)

    本人最近一直在ubuntu,接下来和大家分享我曾经踩过的坑,和一些非常实用的命令知识- 安装中的磁盘分配 一般来说,在linux系统中都有最少两个挂载点,分别是/ (根目录)及 swap(交换分区), ...

  6. YARN 的深入简出

    1.YARN的产生背景 2.YARN的执行流程 3.YARN的概述 4.YARN的环境搭建 5.YARN的架构 6.如何提交作业到YaRN上执行 YARN的产生MapReduce1.x存在多种问题单节 ...

  7. TZOJ 1321 Girls and Boys(匈牙利最大独立集)

    描述 the second year of the university somebody started a study on the romantic relations between the ...

  8. c++ sizeof,strlen, length

    #include <map>#include <iostream>#include <algorithm>#include <functional>#i ...

  9. SqlServer中的数据库分类

    1.系统数据库(中央管理机构):用来管理用户创建用户数据的数据库. 系统数据库中包含如下数据库: (1)master:记录了sqlserver中所有系统级别的信息,包括所有的登录账户.系统配置,还有其 ...

  10. [Z]sql优化

    前言:平常写的SQL可能主要以实现查询出结果为主,但如果数据量一大,就会突出SQL查询语句优化的性能独特之处.一般的数据库设计都会建索引查询,这样较全盘扫描查询的确快了不少.下面总结下SQL查询语句的 ...