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的更多相关文章

  1. 152. Maximum Product Subarray - LeetCode

    Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...

  2. Maximum Product Subarray——LeetCode

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  3. LeetCode Maximum Product Subarray(枚举)

    LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...

  4. 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray

    题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...

  5. LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关

    Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...

  6. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

  7. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

  8. 【刷题-LeetCode】152 Maximum Product Subarray

    Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...

  9. LeetCode_Maximum Subarray | Maximum Product Subarray

    Maximum Subarray 一.题目描写叙述 就是求一个数组的最大子序列 二.思路及代码 首先我们想到暴力破解 public class Solution { public int maxSub ...

随机推荐

  1. day15 CSS JS DOM初探

    居中  line-hight  是上下          text-line  是左右    实现一个返回顶部的功能: 1 先写好CSS 2 写动作JS 写一个悬浮菜单: <!DOCTYPE h ...

  2. Creating Hyperv Agent Installer

    Creating Hyperv Agent Installer   Skip to end of metadata   Created by Anshul Gangwar, last modified ...

  3. webpack vue-cli 常见问题总结

    1. webpack打包压缩 ES6 js..vue报错: ERROR in js/test.js from UglifyJs Unexpected token punc ?(?, expected ...

  4. MySQL误操作后如何快速恢复数据?

    摘要: 利用binlog闪回误操作数据. 基本上每个跟数据库打交道的程序员(当然也可能是你同事)都会碰一个问题,MySQL误操作后如何快速回滚?比如,delete一张表,忘加限制条件,整张表没了.假如 ...

  5. pc端自适应方案

    一.常见处理方式 定宽 电商类.内容为主的网站几乎采用这种方式 1.网易考拉.京东(1190px) 2.知乎(1000px),果壳(1000px),网易新闻(1200px) 媒体查询+定宽 图片类.简 ...

  6. 第二篇:python基础_2

    本篇内容 数字 字符串 元祖 字典 列表 集合 for循环 二进制 字符编码 文件处理 一.数字 1.int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2 ...

  7. 【bzoj3007】拯救小云公主 二分+对偶图+并查集

    题目描述 英雄又即将踏上拯救公主的道路…… 这次的拯救目标是——爱和正义的小云公主. 英雄来到boss的洞穴门口,他一下子就懵了,因为面前不只是一只boss,而是上千只boss.当英雄意识到自己还是等 ...

  8. HDU 5418 Victor and World(状压DP+Floyed预处理)

    Victor and World Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Other ...

  9. BZOJ2132 圈地计划 【最小割】

    题目 最近房地产商GDOI(Group of Dumbbells Or Idiots)从NOI(Nuts Old Idiots)手中得到了一块开发土地.据了解, 这块土地是一块矩形的区域,可以纵横划分 ...

  10. linux系统——fread()与read()函数族区别

    fread与read区别: 1,fread是带缓冲的,read不带缓冲. 2,fopen是标准c里定义的,open是POSIX中定义的. 3,fread可以读一个结构.read在linux/unix中 ...