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.

08/01/2018 update: 在code里面可以用

dp_max[i%2] = max(ma, mi, num)
dp_min[i%2] = min(mi, mi, num)
去代替之前的6行.

这个题目实际上是[LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming的follow up, 有点要注意的是如果我们只用 A[i] 是max value which contains nums[i] for sure,

then A[i] = max(A[i-1] * nums[i], nums[i]), 不够了, 比如说 [2, 3, -2, 4, -1] , 最大值应该为48, 但是我们得到最大值为6, 因为在nums[i] < 0 时, 我们应该将之前的最小值* nums[i] 去得到最大值. 所以有两个dp数组, 一个记录最小值, 一个记录最大值, 每次将最大值和ans比较, 最后返回ans

1. Constraints

1) size >=1 

2)element , integer

2. Ideas

Dynamic Programming        T: O(n)     S; O(1)   using rolling array

3. Code

3.1) S; O(n)

class Solution:
def maxProductSubarry(self, nums):
n = len(nums)
dp_max, dp_min = [0]*n, [0]*n
dp_max[0], dp_min[0], ans = nums[0], nums[0], nums[0]
for i in range(1, n):
if nums[i] > 0:
dp_max[i] = max(dp_max[i-1] * nums[i], nums[i])
dp_min[i] = min(dp_min[i-1] * nums[i], nums[i])
else:
dp_max[i] = max(dp_min[i-1] * nums[i], nums[i])
dp_min[i] = min(dp_max[i-1] * nums[i], nums[i])
ans = max(ans, dp_max[i])
return ans

3.2) S; O(1) using rolling array

class Solution:
def maxProductSubarry(self, nums):
n, n0 = len(nums), nums[0]
dp_max, dp_min, ans = [n0] + [0], [n0] +[0], n0
for i in range(1, n):
num, ma, mi = nums[i], dp_max[(i-1) % 2] * nums[i], dp_min[(i-1) % 2] * nums[i]
if num > 0:
dp_max[i%2] = max(ma, num)
dp_min[i%2] = min(mi, num)
else:
dp_max[i%2] = max(mi, num)
dp_min[i%2] = min(ma, num)
ans = max(ans, dp_max[i%2])
return ans

4. Test cases

 [2, 3, -2, 4, -1]

[LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming的更多相关文章

  1. [LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  2. [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

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

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

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

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

  5. [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  6. [LeetCode] 198. House Robber _Easy tag: Dynamic Programming

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

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

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

  8. [LeetCode] 97. Interleaving String_ Hard tag: Dynamic Programming

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = ...

  9. [LeetCode] 115. Distinct Subsequences_ Hard tag: Dynamic Programming

    Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...

随机推荐

  1. Qt编写百度离线版人脸识别+比对+活体检测

    在AI技术发展迅猛的今天,很多设备都希望加上人脸识别功能,好像不加上点人脸识别功能感觉不够高大上,都往人脸识别这边靠,手机刷脸解锁,刷脸支付,刷脸开门,刷脸金融,刷脸安防,是不是以后还可以刷脸匹配男女 ...

  2. 【SpringCloud微服务实战学习系列】配置详解

    前言 Spring Boot针对常用的开发场景提供了一系列自动化配置来减少原本复杂而又几乎很少改动的模板化配置内容. 一.配置文件 Spring Boot的默认配置文件位置为src/main.reso ...

  3. SharpGL学习笔记(四) 正射投影

    上节谈到投影变换分为透视投影(perspective projection)和正射投影(orthographic projection)两种. 透视投影我们已经介绍过了, 现在谈谈正视投影. 正射投影 ...

  4. 免费的Web服务

    这个网站包括和很多免费的Web服务,比如传说中的天气预报.手机号归属地.IP地址归属地.列车时刻表.邮箱验证.验证码图片生成.还有什么股票,基金 http://www.webxml.com.cn/zh ...

  5. 开发常见错误之 : IMP-00058: 遇到 ORACLE 错误 1691

    IMP-00058: 遇到 Oracle 错误 1691ORA-01691: Lob 段YQPRO.SYS_LOB0000031467C00006$$无法通过128(在表空间YQPRO中)扩展这种情况 ...

  6. 题目1010:A + B(字符串转数字)

    题目链接:http://ac.jobdu.com/problem.php?pid=1010 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  7. MySQL大数据量分页性能优化

    mysql大数据量使用limit分页,随着页码的增大,查询效率越低下. 测试实验 1.   直接用limit start, count分页语句, 也是我程序中用的方法: select * from p ...

  8. 【咸鱼教程】Egret中可长按复制的文本(例如复制优惠码)

    一 实际效果二 实现原理三 源码下载 在egret中实现长按复制文本效果,一般用于复制优惠码什么的. 一 实际效果         二 实现原理 在egret的游戏元素都是绘制在canvas上的,我们 ...

  9. Canvas裁剪Clip和Region、RegionIterator

    extends:http://blog.csdn.net/lonelyroamer/article/details/8349601 裁剪功能由Canvas提供的一系列的clip...方法 和quick ...

  10. DISTINCT 与 GROUP BY 的比较

    看了很多文章,这两个SQL语句在不同的数据库上面的实现上可能有相同或有不同,但是应当要明确它们在功能概念上的区别,最终得出结论: GROUP BY 用来使用聚集函数获得值,比如 AVG, MAX, M ...