【刷题-LeetCode】152 Maximum Product Subarray
- 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数组,分别保存到位置i的最大和最小连续乘积,这是因为最小的乘积可能是负的,如果再乘上一个负数会变成比较大数
\mathrm{dp\_max[i]} = \max\{\mathrm{nums}[i], \mathrm{dp\_max}[i-1]*\mathrm{nums}[i], \mathrm{dp\_min}[i-1]*\mathrm{nums}[i]\}\\
\mathrm{dp\_min[i]} = \min\{\mathrm{nums}[i], \mathrm{dp\_max}[i-1]*\mathrm{nums}[i], \mathrm{dp\_min}[i-1]*\mathrm{nums}[i]\}
\]
对于数组dp_max和dp_min,每次更新只用两个连续的数,因此可以将空间复杂度优化为\(O(1)\)
class Solution {
public:
int maxProduct(vector<int>& nums) {
int pre_f = nums[0], pre_g = nums[0];
int res = pre_f;
for(int i = 1; i < nums.size(); ++i){
int tmp1 = max(nums[i], max(pre_f*nums[i], pre_g*nums[i]));
int tmp2 = min(nums[i], min(pre_f*nums[i], pre_g*nums[i]));
res = max(res, tmp1);
pre_f = tmp1;
pre_g = tmp2;
}
return res;
}
};
【刷题-LeetCode】152 Maximum Product Subarray的更多相关文章
- 求连续最大子序列积 - 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 (最大乘积子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- Java for 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 --------- java
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- C#解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最大乘积子数组
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- Leetcode#152 Maximum Product Subarray
原题地址 简单动态规划,跟最大子串和类似. 一维状态空间可以经过压缩变成常数空间. 代码: int maxProduct(int A[], int n) { ) ; ]; ]; ]; ; i > ...
- 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 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...
随机推荐
- CF1025B Weakened Common Divisor 题解
Content 定义 \(n\) 个数对 \((a_1,b_1),(a_2,b_2),(a_3,b_3),...,(a_n,b_n)\) 的 \(\text{WCD}\) 为能够整除每个数对中至少一个 ...
- LuoguP7285 「EZEC-5」修改数组 题解
Content 有一个长度为 \(n\) 的 \(0/1\) 串,你可以修改当中的一些元素,求修改后最长的连续为 \(1\) 的子串长度减去修改次数的最大值. 数据范围:\(1\leqslant n\ ...
- shell判断新字符串列表是否在老字符串列表中
for sn in `cat 12.30-new`;do if ! [[ `cat 12.30-old` =~ $sn ]];then echo $sn; fi; done
- jQuery Validate验证(判断)某个字段是否通过验证
jQuery Validate 默认只能判断整个表单是否验证通过,但是有时候我们需要对某个字段进行判断 ,可以使用以下方法 var bool=$("整个form表单的ID").va ...
- 平衡二叉树(c++)实现(存在问题:插入节点后,问题:调整树的结构存在问题)
!!版权声明:本文为博主原创文章,版权归原文作者和博客园共有,谢绝任何形式的 转载!! 作者:mohist 更新那时间: 22:13 03-02-2020 逻辑存在问题:插入节点后,调整数的结构不 ...
- 【九度OJ】题目1047:素数判定 解题报告
[九度OJ]题目1047:素数判定 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1047 题目描述: 给定一个数n,要求判 ...
- 解决ubuntu突然无法联网问题
一.问题描述 今天使用笔记本远程办公的时候,突然电脑无法联网了,使用chrome浏览器访问网页出现如下错误 This site can't be reachedwww.baidu.com's serv ...
- [学习笔记] IT项目管理 - 关键路径法
关键路径法 只有项目网络中最常的或者耗时最多的活动完成之后,项目才能结束,这条最长的活动路线就叫关键路径.组成关键路径的活动称为关键活动. 图形表示 最早开始时间ES 工期Duration 最早结束时 ...
- python 插入mysql数据库字符串中含有单引号或双引号报错
出现问题场景:使用mysql数据库管理接口测试用例,新增接口用例时,传入的paras内容,有多层嵌套的时候,就会有["]双引号括住[']单引号的情况,可能在插入单双引号的数据到数据库的时候, ...
- JMeter_实现算法加密
JMeter有两种方法可以实现算法加密 一.使用__digest自带函数 参数说明: Digest algorithm:算法摘要,可输入值:MD2.MD5.SHA-1.SHA-224.SHA ...