http://blog.csdn.net/v_july_v/article/details/8701148

假设数组为a[],直接利用动归来求解,考虑到可能存在负数的情况,我们用Max来表示以a结尾的最大连续子串的乘积值,用Min表示以a结尾的最小的子串的乘积值,那么状态转移方程为:

       Max=max{a[i], Max[i-1]*a[i], Min[i-1]*a[i]};
       Min=min{a[i], Max[i-1]*a[i], Min[i-1]*a[i]};
    初始状态为Max[0]=Min[0]=a[0]。

 #include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
class Solution {
public:
int maxProduct(int A[], int n) {
int *maxArray = new int[n];
int *minArray = new int[n];
maxArray[] = minArray[] = A[];
int result=maxArray[];
for (int i = ; i < n; i++)
{
maxArray[i] = max(max(maxArray[i-]*A[i],minArray[i-]*A[i]),A[i]);
minArray[i] = min(min(maxArray[i-]*A[i],minArray[i-]*A[i]),A[i]);
result = max(result,maxArray[i]);
}
return result;
}
};
int main()
{
Solution s;
int n = ;
int a[] = {,,-,};
cout << s.maxProduct(a,)<<endl;
return ;
}

==============================================================================================

LinkedIn - Maximum Sum/Product Subarray

Maximum Sum Subarray是leetcode原题,跟Gas Station的想法几乎一模一样。解答中用到的结论需要用数学简单地证明一下。

1
2
3
4
5
6
7
8
9
10
11
12
public int maxSubArray(int[] A) {
    int sum = 0;
    int max = Integer.MIN_VALUE;
    for (int i = 0; i < A.length; i++) {
        sum += A[i];
        if (sum > max)
            max = sum;
        if (sum < 0)
            sum = 0;
    }
    return max;
}

Maximum Product Subarray其实只需要不断地记录两个值,max和min。max是到当前为止最大的正product,min是到当前为止最小的负product,或者1。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public int maxProduct(int[] A) {
    int x = 1;
    int max = 1;
    int min = 1;
    for (int i = 0; i < A.length; i++) {
        if (A[i] == 0) {
            max = 1;
            min = 1;
        } else if (A[i] > 0) {
            max = max * A[i];
            min = Math.min(min * A[i], 1);
        } else {
            int temp = max;
            max = Math.max(min * A[i], 1);
            min = temp * A[i];
        }
        if (max > x)
            x = max;
    }
    return x;
}

http://shepherdyuan.wordpress.com/2014/07/23/linkedin-maximum-sumproduct-subarray/

leetcode-Maximum Product Subarray-ZZ的更多相关文章

  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 求最大子数组乘积

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

  4. Leetcode Maximum Product Subarray

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

  5. 152.[LeetCode] Maximum Product Subarray

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

  6. [LeetCode] Maximum Product Subarray 连续数列最大积

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

  7. [leetcode]Maximum Product Subarray @ Python

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

  8. LeetCode Maximum Product Subarray 解题报告

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

  9. LeetCode Maximum Product Subarray 最大子序列积

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

  10. DP Leetcode - Maximum Product Subarray

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

随机推荐

  1. java HelloWorld时报错:"找不到或无法加载主类"问题的解决办法

    学习java的第一天: 当我在做Java入门的时候,根据教程写的第一个Java程序是: public class Hello{ public static void main(String args[ ...

  2. 20190430-screen、client、offset、scroll等JS中各种宽度距离

    参考文献: JavaScript概念之screen/client/offset/scroll/inner/avail的width/left

  3. 转 crs damon can't start 2个例子

    ###sample 1 "node 1 (10.198.127.5): ps -ef|grep ora.crsd root 45613166 47185944 0 10:24:35 pts/ ...

  4. SpringBoot内嵌Tomcat开启APR模式(运行环境为Centos7)

    网上查到的一些springboot内嵌的tomcat开启apr的文章,好像使用的springboot版本较老,在SpringBoot 2.0.4.RELEASE中已经行不通了.自己整理了一下,供参考. ...

  5. 2019年UX设计新趋势

    UX设计总是在不断变化中.最近短短两年的时间里,我们已经看到,很多地方都大规模采用颠覆性技术,比如语音用户界面,混合现实和智能家居设备.设计这些体验的实际过程可能保持不变,但新技术的出现引发了新的行为 ...

  6. jmeter调试脚本之用户自定义变量

    一.用户自定义的变量 用户自定义变量,设置变量名.变量值,就引用变量名执行操作 名称:用户定义变量的描述性名称,显示在左边节点上,并用于命名事务 注释:用户定义变量的注释信息,非必填项 变量名称:定义 ...

  7. 为easyui添加多条件验证

    easyui的验证框架,validatebox不能有效的支持多个条件的验证,比如中文用户名,既要验证其是中文,又要验证其长度不超过6位时便显得很繁琐,需要反复的为easyui添加验证规则. 在此实现一 ...

  8. SpringMVC入门(二)

    使用注解的方式进行Handler的开发   注意:此处只介绍和方式一不同的地方 1.注解的处理器适配器  在spring3.1之前使用org.springframework.web.servlet.m ...

  9. 阿里云服务器(ECS)购买及配置总结

    云服务器是一种简单高效.安全可靠.处理能力可弹性伸缩的计算服务.其管理方式比物理服务器更简单高效.用户无需提前购买硬件,即可迅速创建或释放任意多台云服务器. 目前比较知名的与服务器提供商有:阿里云.百 ...

  10. C#在不同平台下DLL的引用问题

    缘起 很多时候,我们需要引用在不同平台下的DLL,32位(X86)和64位(X64).如果平台错误,在C#中会引发BadImageFormatException异常. 解决思路 我们同时不能添加不同平 ...