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 ...
随机推荐
- git pull免密码拉取
ssh到服务器上,原来基于public/private key pair的方法不好使了. 1.1 创建文件存储GIT用户名和密码 在%HOME%目录中,一般为C:\users\Administrato ...
- Webapp和后端交互检查测试
除了功能,我们可以使用下面方法,查看交互过程,页面不能发现的问题: 什么是json 什么是json,json是什么,json如何使用 JSON是一种取代XML的数据结构,和xml相比,它更小巧但描述能 ...
- git基础之常用操作
一.版本提交: (1)git add 文件名 (2)git commit -m "版本提交信息" 注:git分两个区:工作区+版本库 在电脑中看到的文件夹就是工作区 有一个隐藏的. ...
- leetcode NO.349 两个数组的交集 (python实现)
来源 https://leetcode-cn.com/problems/intersection-of-two-arrays/ 题目描述 给定两个数组,写一个函数来计算它们的交集. 例子: 给定 nu ...
- 翻译MDN里js的一些方法属性
TypeError The TypeError object represents an error when a value is not of the expected type. [TypeEr ...
- List里面的对象被覆盖
对于for循环,当对象创建在for循环外时,list里面的内容会被覆盖··· 解决办法:把对象创建放入for循环里面: 具体原理:若是放到在for外,对象是同一个,放到for到里面,每次都创建一个新的 ...
- [HNOI2007][bzoj1187] 神奇游乐园 [插头dp]
题面: 传送门 给定一个四联通棋盘图,每个格子有权值,求一条总权值最大的回路 思路: 插头dp基础教程 棋盘? 回路? n,m<=10? 当然是插头dp啦~\(≧▽≦)/~ 然后发现这道题并不是 ...
- 0-Android系统各层中LOG的使用
Android系统各层中LOG的使用 , ANDROID_LOG_DEFAULT, /* only for SetMinPriority() */ ANDROID_LOG_VERBOSE, ANDR ...
- canvas游戏开发系列(1):基础知识
canvas基础知识 canvas是什么? canvas是html5的一个元素,可以说他的功能是html元素中最强大的一个. 举个栗子: 第一步:在页面中引入canvas标签,并且设置好宽高背景等样式 ...
- 美食节(bzoj 2879)
Description CZ市为了欢迎全国各地的同学,特地举办了一场盛大的美食节.作为一个喜欢尝鲜的美食客,小M自然不愿意错过这场盛宴.他很快就尝遍了美食节所有的美食.然而,尝鲜的欲望是难以满足的.尽 ...