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. python笔记-for循环的方法

    #!/usr/bin/env python #-*- coding:utf-8 -*- ''' for 语句 格式: for 变量名 in 集合: 语句 逻辑:按顺序取"集合"中的 ...

  2. Ansible学习 安装

    对于运维人员来说,自动化工具是日常工作中比不可少的.Ansible是一个很好的自动化工具. Ansible默认使用SSH协议管理机器,在管理主机上安装Ansible,管理主机和被管理主机只要安装了py ...

  3. php中foreach循环遍历二维数组

    最近在用tp3.2框架,在查询的时候用到了select(),这条语句返回的是二维数组,所以在对返回的数据做处理时,遇到了些麻烦,百度了下foreach,终于用foreach解决了数据的筛选问题 (因为 ...

  4. Codeforces Round #461 (Div. 2) B. Magic Forest

    B. Magic Forest time limit per test 1 second memory limit per test 256 megabytes Problem Description ...

  5. Codeforces Round #460 (Div. 2)-D. Substring

    D. Substring time limit per test3 seconds memory limit per test256 megabytes Problem Description You ...

  6. iview框架 两侧弹框 出现第二层弹框 一闪而过的问题

    分析原因:寡人怀疑可能是,两层弹出框 采用的是一个开关值,发生了覆盖 解决方式 是在第二层弹框外套层计时器 源代码如下: 修改后为:

  7. KNN算法python实现小样例

    K近邻算法概述优点:精度高.对异常数据不敏感.无数据输入假定缺点:计算复杂度高.空间复杂度高适用数据范围:数值型和标称型工作原理:存在一个样本数据集合,也称作训练样本集,并且样本集中每个数据都存在标签 ...

  8. cf976d Degree Set

    ref #include <algorithm> #include <iostream> #include <cstdio> #include <vector ...

  9. Dcgpofix

    TechNet Library Windows Server Windows Server 2012 R2 and Windows Server 2012 Management and Tools C ...

  10. 3、CSS基础 part-1

    1.给body设置颜色 <html> <body text="red"> <p> hello world</p> <p> ...