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(动态规划)的更多相关文章

  1. 152. Maximum Product Subarray动态规划连乘最大子串

    Find the contiguous subarray within an array (containing at least one number)which has the largest p ...

  2. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

  3. 152. Maximum Product Subarray - LeetCode

    Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...

  4. 【刷题-LeetCode】152 Maximum Product Subarray

    Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...

  5. 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray

    题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...

  6. [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  7. [LeetCode]152. Maximum Product Subarray

    This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...

  8. LeetCode 152. Maximum Product Subarray (最大乘积子数组)

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  9. 【LeetCode】152. Maximum Product Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

随机推荐

  1. 系统编码,文件编码,python编码

    系统编码,可以通过locale命令查看(LINUX)https://wiki.archlinux.org/index.php/Locale_(简体中文), centos7 配置文件在/etc/prof ...

  2. python pyenv

    使用pyenv安装多个版本的python 管理多个python环境使用 virtualenv 请看  http://www.cnblogs.com/juandx/p/5357518.html 安装py ...

  3. (原)kenel开机logo的制作

    今天项目需要,需要制作一个kernel的开机logo,所以在rk3288的平台上进行测试一番. 第一步:配置kernel:选上CONFIG_LOGO_LINUX_CLUT224选项 make menu ...

  4. Netty WebSocket 开发

    代码: Server package netty.protocol.websocket.server; import io.netty.bootstrap.ServerBootstrap; impor ...

  5. Elasticsearch学习笔记——分词

    1.测试Elasticsearch的分词 Elasticsearch有多种分词器(参考:https://www.jianshu.com/p/d57935ba514b) Set the shape to ...

  6. 蜕变成蝶~Linux设备驱动之watchdog设备驱动

    看门狗(watchdog )分硬件看门狗和软件看门狗.硬件看门狗是利用一个定时器 电路,其定时输出连接到电路的复位端,程序在一定时间范围内对定时器清零 (俗称 “喂狗”),如果程序出现故障,不在定时周 ...

  7. 蜕变成蝶~Linux设备驱动之字符设备驱动

    一.linux系统将设备分为3类:字符设备.块设备.网络设备.使用驱动程序: 字符设备:是指只能一个字节一个字节读写的设备,不能随机读取设备内存中的某一数据,读取数据需要按照先后数据.字符设备是面向流 ...

  8. 【2019年04月09日】A股净资产收益率ROE最高排名

    个股滚动ROE = 最近4个季度的归母净利润 / ((期初归母净资产 + 期末归母净资产) / 2). 查看更多个股ROE最高排名. 沈阳机床(SZ000410) - 滚动ROE:251.45% - ...

  9. shell脚本学习笔记(符号)

    shell脚本的学习: 1.Shell的作用是解释运行用户的命令,用户输入一条命令,Shell就解释运行一条,这样的方式称为交互式(Interactive),Shell还有 一种运行命令的方式称为批处 ...

  10. 给新手学习Java的建议

    有很多的朋友都在问我一个同样的问题:新手应该如何能学好Java.我做了一个简单的总结,分享给大家: 1-信念:无论你是选择JAVA,C,C#,C++....还是其他的语言编程,信念是第一位,只有相信自 ...