[LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming
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 ...
随机推荐
- Web编辑器的使用
1.复制web编辑器到你的项目中的表现层(UI) 2.添加引用:FredCK.FCKeditorV2.dll到你的项目中来 3.页面中加引用 <%@ Register TagPrefix=&qu ...
- Android 本地搭建Tomcat服务器供真机测试
准备工具:tomcat 环境:win7 + JDK1.8 + tomcat 9.0.13(64bit) 准备工具:tomcat 1.tomcat官网下载 https://tomcat. ...
- Android 验证APK是否已经签名或是否是Debug签名
https://source.android.google.cn/ http://www.android-doc.com/tools/publishing/app-signing.html Signi ...
- VS自动添加头部注释
让VS自动生成类的头部注释,只需修改两个文集即可,一下两个路径下个有一个 Class.cs文件 D:\Program Files (x86)\Microsoft Visual Studio 14.0\ ...
- Elasticsearch学习之深入搜索四 --- cross-fields搜索
1. cross-fields搜索 一个唯一标识,跨了多个field.比如一个人,标识,是姓名:一个建筑,它的标识是地址.姓名可以散落在多个field中,比如first_name和last_name中 ...
- 最小生成树(prime算法 & kruskal算法)和 最短路径算法(floyd算法 & dijkstra算法)
一.主要内容: 介绍图论中两大经典问题:最小生成树问题以及最短路径问题,以及给出解决每个问题的两种不同算法. 其中最小生成树问题可参考以下题目: 题目1012:畅通工程 http://ac.jobdu ...
- HTML5 Canvas 画纸飞机组件
纸飞机模拟一个物体在规定设计轴线偏离方位. //三角形 function DrawTriangle(canvas, A, B, C) { //画个三角形,“A.B.C”是顶点 with (canvas ...
- 思科SVI接口和路由接口区别
Cisco多层交换中提到了一个SVI接口,路由接口.在多层交换机上可以将端口配置成不同类型的接口. 其中SVI接口 类似于 interface Vlan10ip address 192.168.20 ...
- 2-sat入门(tarjan)hdu(3062)
hdu3062 Party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- SpringBoot应用配置常用相关视图解析器
目录 SpringBoot的自动装配装配了视图解析器了吗? SpringBoot使用JSP SpringBoot中使用Thymeleaf SpringBoot中使用Freemark SpringBoo ...