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.

Hide Tags

Array Dynamic Programming

 

    这个问题是给定一个数组,包括正负数和0,求连续子串数字最大的乘积,最容易动态规划,求出整个matrix<i,j> i-th 到 j-th 之间的数字之积,这样方法时间和空间都是O(n2),另一个比较技巧的方法,只需要O(n)时间和O(1)空间,考虑两个0 之间的数字:
,a,b,c,d,e,f,g,h,
    如果a-h 之间的负数个数为偶数,那么结果就是a - h。
    如果负数个数为奇数,假如为d, 那么考虑d 的左边,最大值为a-c,右边为e-h,因为两个部分中的负数个数为偶数。
    这样在奇数情况下,只要d 去最靠近0的那个,便会有一部分是最大值。
    这样只要从左遍历一次,再从右遍历一次便能找出。
逻辑:
  1. 判断输入个数。
  2. 设初始值,当前计算值curval = 1.
  3. 从左遍历数组。
  4. 如果遇到0,判断最大值(eg:-1,0,-2),curval =1,继续。
  5. 如果非0,那么curval 乘以该值,更新返回值。
  6. 从右遍历数组,重复4.5步,判断0就不要需要了。
 #include <iostream>
using namespace std; class Solution {
public:
int maxProduct(int A[], int n) {
if(n<) return A[];
int retMax=A[];
int curval=;
for(int i=;i<n;i++){
if(A[i]==){
if(>retMax) retMax=;
curval=;
continue;
}
curval*=A[i];
if(curval>retMax) retMax=curval;
}
curval = ;
for(int i=n-;i>=;i--){
if(A[i]==){
curval = ;
continue;
}
curval*= A[i];
if(curval>retMax) retMax= curval;
}
return retMax;
}
}; int main()
{
int a[]={-,,-};
Solution sol;
cout<<sol.maxProduct(a,sizeof(a)/sizeof(int))<<endl;
return ;
}

官方解法中结合了求最小值的解答,是一个标准的dp 过程,

f(k) :0-th to k-th 的最大值
g(h):0-th to k-th 的最小值
那么有:
f(k) = max(  f(k-1) * A[k], A[k], g(k-1) * A[k] )
g(k) = min( g(k-1) * A[k], A[k], f(k-1) * A[k] )
 
 
 

[LeetCode] Maximum Product Subarray 连续数列最大积的更多相关文章

  1. LeetCode Maximum Product Subarray(枚举)

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

  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 最大子序列积

    题意:给一个size大于0的序列,求最大的连续子序列之积.(有正数,负数,0) 思路:正确分析这三种数.0把不同的可能为答案的子序列给隔开了,所以其实可以以0为分隔线将他们拆成多个序列来进行求积,这样 ...

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

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

  5. Leetcode Maximum Product Subarray

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

  6. LeetCode Maximum Product Subarray 解题报告

    LeetCode 新题又更新了.求:最大子数组乘积. https://oj.leetcode.com/problems/maximum-product-subarray/ 题目分析:求一个数组,连续子 ...

  7. 152.[LeetCode] Maximum Product Subarray

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  8. [leetcode]Maximum Product Subarray @ Python

    原题地址:https://oj.leetcode.com/problems/maximum-product-subarray/ 解题思路:主要需要考虑负负得正这种情况,比如之前的最小值是一个负数,再乘 ...

  9. DP Leetcode - Maximum Product Subarray

    近期一直忙着写paper,非常久没做题,一下子把题目搞复杂了..思路理清楚了非常easy,每次仅仅需更新2个值:当前子序列最大乘积和当前子序列的最小乘积.最大乘积被更新有三种可能:当前A[i]> ...

随机推荐

  1. swoole 连接池

    proxy_pool.php <?php class ProxyServer { protected $frontends; protected $backends; /** * @var sw ...

  2. PHP导出成PDF功能开发教程

    准备工作 首先查询了相关的类库,有FPDF,zendPDF,TcPDF等等.首先看了下先选择了FPDF,可以说除了中文字符以外没有什么问题,中文乱码而且看了下最新版本没有很好的解决方案,所以只能放弃. ...

  3. 2017 ACM-ICPC EC-Final ShangHai(思维乱搞赛)

    感觉全是思维乱搞题. Gym - 101775J Straight Master 给你n种扑克,你每次可以出连续的3 ~ 5 张,问你能否出完. Sample Input 2 13 1 2 2 1 0 ...

  4. [Uva623]500!(高精)

    Description 求N! \(N \leq 1000\) Sample Input 10 30 50 100 Sample Output 10! 3628800 30! 265252859812 ...

  5. SparkStreaming和Kafka的整合

    当我们正确地部署好Spark Streaming,我们就可以使用Spark Streaming提供的零数据丢失机制.需要满足以下几个先决条件: 1.输入的数据来自可靠的数据源和可靠的接收器: 2.应用 ...

  6. SQLAlchemy Script

    SQLAlchemy: 1.由于sqlalchemy中没有提供choice方法,所以借助SQLAlchemy-Utils组件提供的choice方法 from sqlalchemy_utils impo ...

  7. document.domain跨子域

    document.domain 用来得到当前网页的域名.比如在地址栏里输入: javascript:alert(document.domain); //www.315ta.com 我们也可以给docu ...

  8. IOS开发---菜鸟学习之路--(十二)-利用ASIHTTPRequest进行异步获取数据

    想要实现异步获取的话我这边了解过来有两个非常简单的方式 一个是利用ASIHTTPRequest来实现异步获取数据 另一个则是利用MBProgressHUD来实现异步获取数据 本章就先来讲解如何利用AS ...

  9. Detect Vertical&Horizontal Segments By OpenCV

    Detect Vertical&Horizontal Segments By OpenCV,and Save the data to csv. Steps: Using adaptiveThr ...

  10. 集训队日常训练20181117 DIV2

    大佬们一顿操作猛如虎,拼命AC强啊 4262: 区间异或  Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByteTotal ...