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. linux普通文件权限和系统目录权限的实践及结论

    测试结论:linux普通文件的读.写.执行权限说明 1.可读r:表示具有读取\阅读文件内容的权限 2.可写w:表示具有新增.修改文件内容的权限 1)如果没有r配合,那么vi编辑文件会提示无法编辑(但可 ...

  2. DevOps - 版本控制 - Gogs

    Gogs Gogs官网:https://gogs.io Gogs文档:https://gogs.io/docs Gogs配置文件手册:https://gogs.io/docs/advanced/con ...

  3. JAVA 同步实现原理

    Synchronized的基本使用 Synchronized是Java中解决并发问题的一种最常用的方法,也是最简单的一种方法.Synchronized的作用主要有三个: 确保线程互斥的访问同步代码 保 ...

  4. Shuffle'm Up POJ - 3087(模拟)

    Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15249   Accepted: 6962 Des ...

  5. Fire Game FZU - 2150 (bfs)

    Problem 2150 Fire Game Accept: 3772    Submit: 12868Time Limit: 1000 mSec    Memory Limit : 32768 KB ...

  6. Hive UDTF开发指南

    在这篇文章中,我们将深入了解用户定义表函数(UDTF),该函数的实现是通过继承org.apache.Hadoop.hive.ql.udf.generic.GenericUDTF这个抽象通用类,UDTF ...

  7. RDLC Reporting in Visual Studio 2017

    原文:RDLC Reporting in Visual Studio 2017 Visual Studio 2017 中可以使用 RDLC Reporting 插件来设计报表,SAP Crystal ...

  8. proguaid 混淆代码

    注意:这里有一个坑.就是-ignorewarnings 他老是混淆不了,告诉你不行.其实加上这句话,就可以了. 下面贴一下代码: -injars c:/ceb_lib.jar -outjars c:/ ...

  9. uoj206 [APIO2016]最大差分

    ref #include "gap.h" #include <iostream> #include <cstdio> using namespace std ...

  10. 成为Java高手的25个学习要点

    成为Java高手的25个学习要点 想成为Java大牛吗?不妨来学习这25个要点. 1. 你需要精通面向对象分析与设计(OOA/OOD).涉及模式(GOF,J2EEDP)以及综合模式.你应该了解UML, ...