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 ...
随机推荐
- Android 8 设置蓝牙名称 流程
记录android 8设置蓝牙名称的流程. packages/apps/Settings/src/com/android/settings/bluetooth/BluetoothDeviceRenam ...
- Win7 vs2017 WDK 1803 1809 驱动开发 出错 KMDF
一.编译出错, 1. 包含头文件出错 解决方案: 需要下载1803 的wdk 最新的1809会出一堆错误 安装程序显示是10.0.17134.1安装完成后是10.0.17134.0 2. Inf2C ...
- adb logcat查看某个进程的输出日志
adb logcat查看某个进程的输出日志 adb logcat 默认是没有这个功能的,我实现了一个小bash函数,添加到你$HOME/.bashrc 文件中: # 作用:能够通过进程名显示log # ...
- GoLang之反射
反射 反射(reflect) 所谓反射(reflect)就是能检查程序在运行时的状态. 使用反射的三条定律: 反射可以将“接口类型变量”转换为“反射类型对象”: 反射可以将“反射类型对象”转换为“接口 ...
- python sort和sorted区别。
前者是方法,后者是函数.oop和opp区别的经典体现.好好领会,就能知道什么时候写类什么时候写函数好.
- Centos 下 JProfiler 9.1.1 安装 部署 及 使用
JProfiler[1] 是一个商业授权的Java剖析工具,由EJ技术有限公司,针对的Java EE和Java SE应用程序开发的.它把CPU.执行绪和内存的剖析组合在一个强大的应用中.JProfil ...
- gitlab-ci + k8s 之gitlab-ci(一)
目前常用的持续集成工具主要是jenkins与gitlab-ci ,我已在另一博文中详细记录了jenkins部署过程(其中包括gitlab的搭建),此篇介绍gitlab-ci的使用. 背景介绍 GitL ...
- ZooKeeper注册中心安装详细步骤(单节点)
安装 Dubbo 注册中心 Dubbo 建议使用 Zookeeper 作为服务的注册中心. 注册中心服务器(192.168.3.71)配置,安装 Zookeeper: 1. 修改操作系统的/etc/h ...
- Windows 环境下 wampserver 与 phpStudy 的环境配置
一. wamperserver 1.下载好安装到本地指定目录,官网下载地址 http://www.wampserver.com 2.根据自己实际的安装路径,D:\pc\wampserver\wamp ...
- Vs code常用插件
Vs code常用插件 1.View In Browser 由于 VSCode 没有提供直接在浏览器中打开文件的内置界面,所以此插件在快捷菜单中添加了在默认浏览器查看文件选项,以及在客户端(Firef ...