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.

看到这道题,很明显的一套动态规划类型的题目,和最长升序子序列之类的十分类似,那么首先就是想递推公式。

思路①:

subarray[i][j]表示数组中从第 i 到第 j 位置的数字组成的子数组的乘积。因此我们有如下递推公式:

subarray[i][j] = subarray[i][j-1] * array[j]

很显然,这种做法需要一个二重循环,时间复杂度是O(n^2)

思路②:

因为考虑到,最大的一个子数组最大乘积可以分为两种情况:

A.之前的子数组乘积为负,当前的数字也为负,相乘为一个大正数

B.之前的子数组乘积为正,当前的数字也是正,相乘为一个正数

考虑到上面的两种情况,因此我们需要保存两个数组,分别记录当前的最大正整数乘积和最小负整数乘积

positive_subarray[i]:表示数组前i个数之前的最大相乘正值(包括第 i 个数)

negative_subarray[i]:表示数组前i个数之前的最大相乘负值(包括第 i 个数)

所以有:

当array[i] >= 0 时:

positive_subarray[i] = max( positive_subarray[i-1]*array[i], array[i] )

negative_subarray[i] = negative_subarray[i-1]*array[i]

当array[i] < 0 时:

positive_subarray[i] = negative_subarray[i-1]*array[i]

negative_subarray[i] = min( positive_subarray[i-1]*array[i], array[i])

这样程序的复杂度就降低到了O(n)

代码如下:

int  maxProduct(vector& nums)
{
  if(nums.empty())
    return 0;
  vector positive_subarray = vector(nums.size(),0);
  vector negative_subarray = vector(nums.size(),0);
  int max_product;
  int len = nums.size();
  if(nums[0] < 0)
    negative_subarray[0] = nums[0];
  else
    positive_subarray[0] = nums[0];
  max_product = nums[0];
  for(int i=1;i
  {
    if(nums[i]>=0)
    {
      positive_subarray[i] = max(positive_subarray[i-1]*nums[i],nums[i]);
      negative_subarray[i] = negative_subarray[i-1]*nums[i];
    }
    else
    {
      positive_subarray[i] = negative_subarray[i-1]*nums[i];
      negative_subarray[i] = min(positive_subarray[i-1]*nums[i],nums[i]);
    }
    if(positive_subarray[i]>max_product)
      max_product = positive_subarray[i];
  }
  return max_product;
}

  

Maximum Product Subarray的更多相关文章

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

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

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

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

  3. LeetCode Maximum Product Subarray(枚举)

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

  4. LeetCode_Maximum Subarray | Maximum Product Subarray

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

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

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

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

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

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

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

  8. 152. Maximum Product Subarray - LeetCode

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

  9. [LeetCode] Maximum Product Subarray 求最大子数组乘积

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

  10. [LeetCode]152. Maximum Product Subarray

    This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...

随机推荐

  1. node.js环境

    1.首先获得node环境 node -v; 2.判断node包管理器的版本 npm-v;查看node包管理器的版本; 3.npm install -g express 安装node.js的web开发框 ...

  2. 如何给main传参数

    main 函数的参数有连个argc argcv[]  argc 是参数个数 argcv是参数的数组指针,且argcv的第一个参数是默认程序路径加程序名 给main传参数,需要在命令行启动程序时设置 如 ...

  3. xcode国际化工具genstrings体验总结

    genstrings是苹果推出的一个用于自动从代码的nslocalizedstring等提取生成国际化字符串的工具: xcode的国际化文件方案一直以来都不太智能,我记得很久以前.strings文件库 ...

  4. 安装VS 2015完成后,VS2012 打开报错

    安装VS 2015完成后,VS2012 打开报错 打开VS2012Web项目,弹出错误提示: asp.net 4.0 has not been registered on the web server ...

  5. CSS3媒体查询能检测到的特性小结

    CSS3的Media Queries能够检测到的特性总结: 视口(viewport)解释地址:http://baike.baidu.com/view/1522985.htm width:视口宽度的检测 ...

  6. 调用CachedRowSetImpl类时出现错误

    调用CachedRowSetImpl类时,出现以下错误: Access restriction: The type CachedRowSetImpl is not accessible due to ...

  7. VS2013 GIT 克隆远程仓库

    1.配置本地GIT 工具->选项->源代码管理,选择GIT 2.打开团队资源管理器,找到GIT克隆选项 3.单击克隆,在输入框内输入远程仓库地址,然后单击克隆即可 GIT 插件配置:参考  ...

  8. java编程题

    第一题:输入字符串长度len1,字符串s1,字符串长度len2,字符串s2.从后向前比较,以最短字符串为标准,输出不同的元素的个数. 例如:    输入:s1="1,3,5"   ...

  9. buildroot--uboot&kernel&rootfs全编译工具

    参考: http://www.crifan.com/files/doc/docbook/buildroot_intro/release/html/buildroot_intro.html https: ...

  10. 修复Magento SQLSTATE[23000]: Integrity constraint

    magneto在意外情况下报错Magento SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry,出现这个问题最 ...