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 ...
随机推荐
- Android 显示和隐藏软键盘的方法
前言:因为项目要求做一个类似贴吧一样的东西,可以评论,所以必不可少地需要用到软键盘的隐藏和显示. Step 1 废话不多说,先上封装好的代码. public class CommonUtils ...
- HTML5——7个最牛的HTML5移动开发框架
月的iPhoneDevCamp上写成的.创建它的一个主要动力是基于一个几乎每一个单独的iPhone开发新手都要面对的简单事实:Objective-C是一个对Web开发人员来说非常陌生的环境,并且Web ...
- IOS开发---菜鸟学习之路--(六)-UITableView几个方法的使用说明
对于UITableView的基础使用我这边就不做重复介绍了 我重点就来介绍下如何实现大部分新闻的界面.也就是第一条记录显示大图片下面加一段文字说明 然后剩下来的内容全部显示为文字图片的格式 其实要做到 ...
- html调用commonjs规范的js
a.js define(function(require, exports, module) { var test = function(){ console.log("hello worl ...
- 【Generate Parentheses】cpp
题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...
- 【Letter Combinations of a Phone Number】cpp
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- Robotium测试报告的生成方法(上)
7.1 使用junit-report生成报告 这个是参考网上的:http://www.xuebuyuan.com/2148574.html,经我个人验证是可行的方法,网上写的挺详细的,不过有些不太清楚 ...
- Python+Selenium练习篇之9-清除文本方法
在前面的基础篇的最后一篇,我们用到了输入字符和点击按钮这样的操作.用send_keys()来输入字符串到文本输入框这样的页面元素,用click()来点击页面上支持点击的元素.有时候,我们需要清除一个文 ...
- 最少的硬币数量组合出1到m之间的任意面值(贪心算法)
题目描述: 你有n种不同面值的硬币,每种面值的硬币都有无限多个,为了方便购物,你希望带尽量少的硬币,并且要能组合出 1 到 m 之间(包含1和m)的所有面值. 输入描述: 第一行包含两个整数:m ,n ...
- 拼多多2018校招编程题汇总 Python实现
题目原址 列表补全 在商城的某个位置有一个商品列表,该列表是由L1.L2两个子列表拼接而成.当用户浏览并翻页时,需要从列表L1.L2中获取商品进行展示.展示规则如下: 用户可以进行多次翻页,用offs ...