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. 服务端模拟http服务请求客户端

    try { InputStream in = req.getInputStream(); int i = -1; ByteArrayOutputStream out = new ByteArrayOu ...

  2. HttpClient登陆后获取并携带cookies发起请求

    最近项目中,用到了登陆后获取并携带cookies发起请求的业务场景,现总结写出来备忘一下. 1.定义存取cookies信息的全局变量 public class HttpUtil { /** * 用来存 ...

  3. [原]SuperMap GIS(JavaScript) 拉框放大和缩小功能实现

    版权声明:本文为博主原创文章,未经博主允许不得转载. var ZoomControl; /* * 拉框缩小 */ function ZoomOut(){ if(ZoomControl==null||Z ...

  4. (转)浅谈 Linux 系统中的 SNMP Trap

    原文:https://www.ibm.com/developerworks/cn/linux/l-cn-snmp/index.html 简介 本文讲解 SNMP Trap,在介绍 Trap 概念之前, ...

  5. (转)tune2fs命令详解

    tune2fs命令详解(原创) 原文:http://czmmiao.iteye.com/blog/1749232 tune2fs简介 tune2fs是调整和查看ext2/ext3文件系统的文件系统参数 ...

  6. 资料整理:基于node push server实现push notification

    chat example based on ionic/ socket.io/ redis https://github.com/jbavari/ionic-socket.io-redis-chat ...

  7. 开源移动端IM比较SipDroid,IMSDroid,CSipsimple,Linphone,webrtc

    最新要做一个移动端视频通话软件,大致看了下现有的开源软件 一) sipdroid1)架构sip协议栈使用JAVA实现,音频Codec使用skype的silk(Silk编解码是Skype向第三方开发人员 ...

  8. STM32F407 使用HAL库延时微妙实现方法(附CubeMX配置过程)

    STM32F407 使用HAL库延时微妙实现方法(STM32CubeMX配置) 作者 : 李剀出处 : https://www.cnblogs.com/kevin-nancy/p/10696681.h ...

  9. dll和so文件区别与构成

    http://www.cnblogs.com/likwo/archive/2012/05/09/2492225.html 动态链接,在可执行文件装载时或运行时,由操作系统的装载程序加载库.大多数操作系 ...

  10. Linux定时增量更新文件--转

    http://my.oschina.net/immk/blog/193926 动机与需求:现在有两台服务器A和B,由于A的存储随时会挂(某些原因),所以需要B机器上有A的备份,并且能够与A同步更新 一 ...