Maximum Product Subarray - LeetCode
Find the contiguous subarray within an array (containing at least one number) which has the largest product.
For example, given the array [2,3,-2,4],
the contiguous subarray [2,3] has the largest product = 6.
思路:DP。这里我们实际上不需要O(n)的空间,只需要用变量将遍历到的最大值记录下来就可以。
其中,因为求的是数的乘积,有可能原本两个都是负的值相乘后会成为最大的乘积。
因此,我们需要维护两个变量,MaxPre和MinPre。
MaxPre为最后一位是当前位置前一位的区间最大乘积。
MinPre为最后一位是当前位置前一位的区间最小乘积。
之后,将当前位置考虑在内的话,有:
MaxSoFar = max(max(MaxPre * nums[i], MinPre * nums[i]), nums[i]);
MinSoFar = min(min(MinPre * nums[i], MaxPre * nums[i]), nums[i]);
然后用MaxResFound记录下迭代过程中MaxSoFar的最大值,即为结果。
class Solution {
public:
int maxProduct(vector<int>& nums) {
int n = nums.size();
if (n == ) return ;
int MaxSoFar, MinSoFar, MaxResFound, MaxPre, MinPre;
MaxPre = MinPre = MaxSoFar = MinSoFar = MaxResFound = nums[];
for (int i = ; i < n; i++)
{
MaxSoFar = max(max(MaxPre * nums[i], MinPre * nums[i]), nums[i]);
MinSoFar = min(min(MinPre * nums[i], MaxPre * nums[i]), nums[i]);
MaxResFound = max(MaxResFound, MaxSoFar);
MaxPre = MaxSoFar;
MinPre = MinSoFar;
}
return MaxResFound;
}
};
Maximum Product Subarray - LeetCode的更多相关文章
- 152. Maximum Product Subarray - LeetCode
Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...
- Maximum Product Subarray——LeetCode
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode Maximum Product Subarray(枚举)
LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关
Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
- 【刷题-LeetCode】152 Maximum Product Subarray
Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...
- LeetCode_Maximum Subarray | Maximum Product Subarray
Maximum Subarray 一.题目描写叙述 就是求一个数组的最大子序列 二.思路及代码 首先我们想到暴力破解 public class Solution { public int maxSub ...
随机推荐
- java十分钟速懂知识点——引用
一.由健忘症引起的问题 今天闲来没事在日志中瞟见了个OutOfMemoryError错误,不由得想到前一段时间看到一篇面经里问到Java中是否有内存泄露,这个很久以前是留意过的,大体记得内存溢出和内存 ...
- Redis实现之字典跳跃表
跳跃表 跳跃表是一种有序数据结构,它通过在每个节点中维持多个指向其他节点的指针,从而达到快速访问节点的目的.跳跃表支持平均O(logN).最坏O(N)的时间复杂度查找,还可以通过顺序性操作来批量处理节 ...
- Django 三—— Form组件
内容概要: 1.Django Form如何自定义验证字段 2.Django Form如何动态的显示数据库中新插入的数据 3.Tyrion Django的Form(用于验证用户请求合法性的一个组件) D ...
- 史上最权威的 Activiti 框架学习
Activiti5 是 由 Alfresco 软件在 2010 年 5 月 17 日发布的业务流程管理( BPM) 框架,它是覆盖了业务流程管理.工作流.服务协作等领域 的一个开源的.灵活的. ...
- 《HTTP协议详解》读书笔记---请求篇之情求方法
之前对于网络这一块不是很清楚,值知道TCP/IP协议,三次握手四次握手之类的很笼统,零碎的知识,现在打算系统学习下网络相关的知识,先从http协议开始. 本人,还是新手,对于一些知识如果有理解错误的, ...
- django orm 基本Field介绍
ORM:object relational mapping,对象关系映射 django中使用原生sql的弊端: 1.SQL语句重复率很高,利用率不高 2.如果业务逻辑生变,原生SQL更改起来比较多 3 ...
- PAT1024
科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+-][0-9]+,即数字的整数部分只有1位,小数部分至少有1位 ...
- POJ 1149 PIGS(Dinic最大流)
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20738 Accepted: 9481 Description ...
- BZOJ1801 [Ahoi2009]chess 中国象棋 【dp】
题目 在N行M列的棋盘上,放若干个炮可以是0个,使得没有任何一个炮可以攻击另一个炮. 请问有多少种放置方法,中国像棋中炮的行走方式大家应该很清楚吧. 输入格式 一行包含两个整数N,M,中间用空格分开. ...
- 刷题总结——纸带(NOIP赛前模拟)
题目: 有一个无限长的纸带··上面被划分为若干个格子··现在进行N次操作,第i次操作在L到R上擦出曾经写上的数字(如果有),并写上数字i,询问最终可以看到多少个数字 N小于10^6 题解: 首先毫无疑 ...