152. Maximum Product Subarray(动态规划)
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.
由于负数的存在,需要同时保存当前最大值和当前最小值,所以需要维护两个DP表,可以分别表示为dp_min和dp_max。所以即为dp_max里的最大值。
需要维护的当前最大值和当前最小值,都是在dp_min[i-1] * A[i],dp_max[i] * A[i],和A[i]这三者里面取一即可。有了这个只关乎最终状态,不关乎过程细节的结论,解题过程可以大大简化。
class Solution {
public:
int maxProduct(vector<int>& nums) {
int n = nums.size();
if(n==) return ;
vector<int>dp_max(n,nums[]);
vector<int>dp_min(n,nums[]);
int res_val = nums[];
for(int i =;i<n;i++){
dp_max[i] = std::max( std::max(dp_max[i-]*nums[i],dp_min[i-]*nums[i]), nums[i]);
dp_min[i]= std::min( std::min(dp_min[i-]*nums[i],dp_max[i-]*nums[i]), nums[i]);
}
for(int i =;i<n;i++)
res_val = std::max(dp_max[i],res_val);
return res_val;
}
};
思考以上DP解法的空间开销过大的原因,是因为保存了整个DP表。其实整个过程中,获得dp[i]的值只需要dp[i-1]的值,所以是不需要保存整个DP表的。
这样一来,DP可以用滚动数组进行优化。简单的写法其实就是设一对prevMin/prevMax表示上一个值,以及还有一对curMin/curMax表示当前值。
class Solution {
public:
int maxProduct(vector<int>& nums) {
int n = nums.size();
if(n==) return ;
int dp_max_pre = nums[];
int dp_min_pre = nums[];
int dp_max;
int dp_min;
int res_val = nums[];
for(int i =;i<n;i++){
dp_max = std::max( std::max(dp_max_pre*nums[i],dp_min_pre*nums[i]), nums[i]);
dp_min= std::min( std::min(dp_max_pre*nums[i],dp_min_pre*nums[i]), nums[i]);
res_val =std::max(res_val,dp_max);
dp_max_pre = dp_max;
dp_min_pre = dp_min;
}
return res_val;
}
};
152. Maximum Product Subarray(动态规划)的更多相关文章
- 152. Maximum Product Subarray动态规划连乘最大子串
Find the contiguous subarray within an array (containing at least one number)which has the largest p ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
- 152. Maximum Product Subarray - LeetCode
Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...
- 【刷题-LeetCode】152 Maximum Product Subarray
Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- [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
This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...
- LeetCode 152. Maximum Product Subarray (最大乘积子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【LeetCode】152. Maximum Product Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
随机推荐
- gsoap使用总结
WebService.soap.gsoap基本概念 WebService服务基本概念:就是一个应用程序,它向外界暴露出一个可以通过web进行调用的API,是分布式的服务组件.本质上就是要以标准的形式实 ...
- Sigmoid函数简介
Sigmoid函数是一个在生物学中常见的S型的函数,也称为S型生长曲线.[1] 中文名 Sigmoid函数 外文名 Sigmoid function 别名 S型生长曲线 Sigmoid函数由下列公式定 ...
- Windows SDK DDK WDK (Windows Driver Kit) 区别
首先,先从基础的东西说起,开发WINDOWS下的驱动程序,需要一个专门的开发包,如:开发JAVA程序,我们可能需要一个JDK,开发WINDOWS应用程序,我们需要WINDOWS的SDK,现在开发WIN ...
- 搭建Pypi转发服务
有时候有些正式环境的机器,不能访问外网,就只能在能访问外网的机器上搭建一个转发服务. 一.安装包 pip install flask_pypi_proxy flask_pypi_proxy 二.启动 ...
- 【中间件安全】Jboss安全加固规范
1. 适用情况 适用于使用Jboss进行部署的Web网站. 适用版本:5.x版本的Jboss服务器 2. 技能要求 熟悉Jboss安装配置,能够Jboss进行部署,并能针对站点使用Jboss进行安全加 ...
- git bash here右键菜单
Windows Registry Editor Version 5.00 ; Open files[HKEY_CLASSES_ROOT\*\shell\gitbash]@="gitbash& ...
- css背景图撑开盒子高度
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 添加ll命令
$ vim ~/.bashrcalias ll='ls -l' #加入此行 ps:加入后肯能无法当场起作用,执行该句: source ~/.bashrc
- Solve Error: 'NSInvalidArgumentException', reason: '-[UITableView mas_makeConstraints:]: unrecognized selector sent to instance 0x7fa5c402fa00'
下面是iOS开发用第三方库可能出现的错误,及其解决方法: 1. 'NSInvalidArgumentException', reason: '-[UITableView mas_makeConstra ...
- Educational Codeforces Round 1
598A - Tricky Sum 20171103$$ans=\frac{n(n+1)}{2} - 2\sum_{k=0}^{\left \lfloor \log_2 n \right \rf ...