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的更多相关文章
-
[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 ...
-
[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 ...
-
[LeetCode] 152. Maximum Product Subarray 求最大子数组乘积
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
-
LeetCode 152. Maximum Product Subarray (最大乘积子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
-
[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 ...
-
[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 ...
-
求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
-
[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 = ...
-
[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 ...
随机推荐
-
图片标签img中,为什么使用alt属性没用
alt属性 alt属性是为了给那些不能看到你文档中图像的浏览者提供文字说明的.所以alt属性的本意是用于替换图像,而不是为图像提供额外说明的,但是,在ie浏览器中,alt属性会变成文字提示,这本身是一 ...
-
HTTP协议中的1xx,2xx,3xx,4xx,5xx状态码分别表示什么,列举常见错误码及含义
转自:http://m.blog.csdn.net/blog/u013857407/21741847 HTTP协议状态码,是指在HTTP协议运作中由客户端发出请求连接,服务端建立连接,客户端发出HTT ...
-
HDU 4712Hamming Distance(随机函数运用)
Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
-
ThinkPhp5源码剖析之Cache
为什么需要Cache(缓存)? 假设现在有一个小说网,有非常多的读者,有一篇新的章节更新了,那么可能一分钟内有几万几十万的访问量. 如果没有缓存,同样的内容就要去数据库重复查询,那可能网站一下就挂掉了 ...
-
magento: Your web server is configured incorrectly. As a result, configuration files with sensitive information are accessible from the outside 解决方案
在linux(以UBUNTU, CENTOS为例)下安装完成magento时,在进入后台时, 有些童鞋可能会发现有如下的提示: Your web server is configured incorr ...
-
最简单的Web Service实现
概述 这里提供一个最简单的Web Service的实现,基于JAX-WS.除了jdk不需要任何其他jar包,使用Eclipse提供的Web Services Explorer访问服务. 服务端的实现 ...
-
PAT 天梯赛 L1-018. 大笨钟 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-018 AC代码 #include <iostream> #include <cstdio&g ...
-
.NET 单点登录解决方案
这里指的单点,泛指在WEB服务端,一个账户同一时刻只能存在一个票据! 大家开发中可能都碰到的一个问题,怎么使同一个用户,在同一时间内只允许登录一次. 很多人都会想到在数据库中用一个标识字段,登录进去置 ...
-
spring boot 很好的文章
http://blog.csdn.net/isea533/article/details/50278205
- Python快速学习-基础语法