Maximum Product Subarray

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;
}

  

时间: 2024-12-28 08:16:09

Maximum Product Subarray的相关文章

leetcode_152题——Maximum Product Subarray(动态规划)

Maximum Product Subarray Total Accepted: 33022 Total Submissions: 170218My Submissions Question Solution 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

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

Add Date 2014-09-23 Maximum Product Subarray 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 = 

LeetCode Maximum Product Subarray(枚举)

LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, you should determine what is the value of the maximum positive product involving consecutive terms of S. If you cannot find a positive sequence, you

LeetCode:Maximum Product Subarray

题目:Maximum Product Subarray 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. 这道题属于动态规划的题型,

LeetCode: Maximum Product Subarray &amp;&amp; Maximum Subarray

Maximum Product Subarray Title: 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. 对于Product

152. Maximum Product Subarray(js)

152. Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1: Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product

刷题152. Maximum Product Subarray

一.题目说明 题目152. Maximum Product Subarray,给一列整数,求最大连续子序列,其乘积最大.难度是Medium! 二.我的解答 这个题目,用双重循环就可以了. class Solution{ public: int maxProduct(vector<int>& nums){ if(nums.size()<=1) return nums[0]; product = INT_MIN; int len = nums.size(); for(int i=0;

【LeetCode】152. Maximum Product Subarray

题目: 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. 题解: 先暴力解,遍历所有组合,更新最大值.很显然得超时. Solution

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. 这道题和Maximum Sum Subarray类似,也是考虑什么时候可以取得最大值

[C++]LeetCode: 96 Maximum Product Subarray(动态规划)

题目: 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. 思路: 这道题和Maximum Subarray解法类似,维护两个变量来动