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. JQ第二天

    一.属性.表单过滤选择器 $("div[id]")选取有id属性的<div>//$("div [id]")有空格表示div层当中有id属性的元素 $ ...

  2. oracle数据库使用三个月的总结

    存储过程定义,举个例子如下: create or replace procedure test_person(id in Number, Ename In Varchar2, age In Varch ...

  3. 后台系统组件:一丶bootstrap table

    http://www.cnblogs.com/landeanfen/p/4976838.html (bootstrap table) http://www.cnblogs.com/landeanfen ...

  4. jqxComboBox

    var uptype_source = [{ "TYPE_DESC": "总额度", " }, { "TYPE_DESC": &q ...

  5. GIT使用笔记-fatal:multiple stage entries for merged file处理办法

    该错误是在cherry-pick时出现 无法确定冲突原因 分支无法checkout ,reset等等全都失效 在网上给出的解决办法全部都是 rm .git/index git add -A git c ...

  6. sqlserver锁表、解锁、查看销表 (转载)

    sqlserver中怎么锁表.解锁.查看销表呢,下面我以三个不同的实例给各位朋友详细介绍一下有需要的朋友可参考一下. 更多详细内容请查看:http://www.111cn.net/database/O ...

  7. 使用HTTPS网站搭建iOS应用内测网站(OTA分发iOS应用)

    为什么要搭建应用内测网站呢? 1.AppStore的审核速度比较慢,万一被拒,还得等,而且一旦发布,任何人都可以下载,而有些时候只有老板想知道最新的修改是否符合要求,万一不符合要求呢?又要修改了. 2 ...

  8. Javascript Promise入门

    是什么? https://www.promisejs.org/ What is a promise? The core idea behind promises is that a promise r ...

  9. centos 安装pptp

    1. 安装依赖 ppp yum -y install ppp 2. 编译安装pptpd wget http://jaist.dl.sourceforge.net/project/poptop/pptp ...

  10. svg学习(九)path

    <path> 标签用来定义路径. 下面的命令可用于路径数据: M = moveto L = lineto H = horizontal lineto V = vertical lineto ...