[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 ...
随机推荐
- springbatch---->springbatch的使用(二)
这里我们对springbatch做一个比较深入的学习例子,解压文件,读取文件内容过滤写入到数据库中.如果你掉进了黑暗里,你能做的,不过是静心等待,直到你的双眼适应黑暗. springbatch的使用案 ...
- php curl-class post
use \Curl\Curl; $curl = new Curl();$curl->setHeader('Content-Type', 'application/json');$curl-> ...
- (转载)解决AndroidStudio导入项目在 Building gradle project info 一直卡住
源地址http://blog.csdn.net/yyh352091626/article/details/51490976 Android Studio导入项目的时候,一直卡在Building gra ...
- Java中的一些性能监控和故障分析工具
这些工具都在JDK的bin目录下,如果配置了java的环境变量,可以直接在命令行里调用这些小工具 jps 查看java进程信息 jstat java虚拟机状态监控工具 jstat -gc(或 -gcn ...
- facebook login issue
If enable the facebook account in settings, when change account can't open the session. -(void)fbRes ...
- Linux下openoffice与SWFtools的安装
第一部份:OpenOffice的安装 1.Openoffice的官网:http://www.openoffice.org/download/index.html 选择Linux 64-bit(x860 ...
- 在pycharm中运行nose测试框架
之前一直在pydev上或命令行上运行nosetests. pycharm上如果运行nosetests,在看了管网后,总结果如下: 全新的pycharm: 填加完成后,打开你要的脚本,运行,即可以以no ...
- Nginx通过header转发
假设添加自定义头 "my-header",当"my-header"等于test时,转发到192.168.1.113 请求如下 wget --header=&qu ...
- return 通过文件后缀名得到的函数字符串
<?php//图片处理工具类class Image{//属性private $thumb_width; //缩略图的宽private $thumb_height;//错误属性public $th ...
- logstash实战input插件syslog
vim /etc/logstash/conf.d/syslog.conf input{ syslog{ type => "system-syslog" port => ...