[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 ...
随机推荐
- 子页面iframe跨域执行父页面定义的JS方法
问题需求:父页面与子页面iframe跨域嵌套,子页面要触发父页面所定义的js方法.父子页面的数据传递. 下文中会用到一些文件:父页面: parent.html嵌在父页面的子iframe页面:child ...
- 原生js--addEventListener和attachEvent的区别
add 代表addEventListener, att代表attachEvent(IE8-) 1.add接受三个参数,att接受两个参数(根本原因是IE不支持事件捕获) 2.add的第一个参数(事件类 ...
- tar解压包的时候出现错误 gzip: stdin: not in gzip format
在Linux环境下,通过tar -zxvf 命令解压文件时遇到”gzip: stdin: not in gzip format“等错误:如图所示 root@cmfchina:/usr/java# ta ...
- set数组去重
new Set const arr = [{name:"wo"},{name:"shi"},{name:"wo"}] console.log ...
- 被C语言操作符优先级坑了
今天有一个枚举的题目的代码是这样的: 重点在于maxXor这个函数的实现,枚举两个数字,其中maxr保存了最大值的 i 异或 j , 可是这个程序执行结果大大出乎意外-_-. 然后就把 i 异或 j ...
- C程序设计语言习题(3-3)
编写函数expand(s1,s2), 将字符串s1中类似于a-z一类的速记符号在字符串s2中扩展为等价的完整列表abc……xyz.该函数可以处理大小写字母和数字,并可以处理a-b-c.a-z0-9与a ...
- iOS 8 AutoLayout与Size Class自悟
前言 iOS8和iPhone6发布已经过去蛮久了,广大的果粉终于迎来了大屏iPhone,再也不用纠结为大屏买三星舍苹果了…但是对于iOS开发人员来说,迎来了和Android开发开发一样的问题—> ...
- Express 4.x Node.js的Web框架----《转载》
本文使用node.js v0.10.28 + express 4.2.0 1 Express概述 Express 是一个简洁而灵活的node.js的MVC Web应用框架,提供一系列强大特性创建各种W ...
- Java.Util.List(List接口)
equals方法 equals(Object o) 方法用来比较指定的对象与列表是否相等,当且仅当指定的对象也是一个列表.两个列表有相同的大小,并且两个列表中的所有相应的元素对相等时才返回 true. ...
- C++和Java中枚举enum的用法
在C++和java中都有枚举enum这个关键字,但是它们之间又不太一样.对于C++来说,枚举是一系列命名了的整型常量,而且从枚举值转化为对应的整型值是在内部进行的.而对于Java来说,枚举更像一个类的 ...