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.


思路:类似最大和连续子序列那样,不过除了记录最大乘积,我们还要记录最小的乘积。这里我分三种情况:

1.A[i]>0。

2.A[i]<0。p[i].max=Max(p[i-1].min*A[i],A[i]);也就是看看前i-1项的最小值是不是负数,若是负数,则p[i].max=p[i-1]*A[i](负负得正);若不是负数,则p[i]=A[i]。

3.若A[i]=0,则以0为结尾的序列的乘积的最大值和最小值一定都是0。

代码:

class Solution {
private:
struct pro{
int max;
int min;
};
public:
int Max(int a,int b){
return a>b?a:b;
}
int Min(int a,int b){
return a<b?a:b;
}
int maxProduct(int A[], int n) {
struct pro p[n];
p[].max=A[];
p[].min=A[];
for (int i=;i<n;++i)
{
if(A[i]>){
p[i].max=Max(p[i-].max*A[i],A[i]);
p[i].min=Min(p[i-].min*A[i],A[i]);
}
else if(A[i]<){
p[i].max=Max(p[i-].min*A[i],A[i]);
p[i].min=Min(p[i-].max*A[i],A[i]);
}
else{
p[i].max=;
p[i].min=;
}
}
int res=p[].max;
for (int i=;i<n;++i)
{
if(p[i].max>res)
res=p[i].max;
}
return res;
}
};

Maximum Product Subarray(最大连续乘积子序列)的更多相关文章

  1. lintcode 中等题 :Maximum Product Subarray 最大连续乘积子序列

    题目 乘积最大子序列 找出一个序列中乘积最大的连续子序列(至少包含一个数). 样例 比如, 序列 [2,3,-2,4] 中乘积最大的子序列为 [2,3] ,其乘积为6. 解题  法一:直接暴力求解 时 ...

  2. Maximum Product Subarray 最大连续乘积子集

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

  3. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

  4. LeetCode 152. Maximum Product Subarray (最大乘积子数组)

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

  5. 46.Maximum Product Subarray(最大乘积子数组)

    Level:   Medium 题目描述: Given an integer array nums, find the contiguous subarray within an array (con ...

  6. 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray

    题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...

  7. LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关

    Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...

  8. LeetCode Maximum Product Subarray(枚举)

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

  9. LeetCode_Maximum Subarray | Maximum Product Subarray

    Maximum Subarray 一.题目描写叙述 就是求一个数组的最大子序列 二.思路及代码 首先我们想到暴力破解 public class Solution { public int maxSub ...

随机推荐

  1. LN : leetcode 312 Burst Balloons

    lc 312 Burst Balloons 312 Burst Balloons Given n balloons, indexed from 0 to n-1. Each balloon is pa ...

  2. leetcode764 Largest Plus Sign

    思路: 首先使用dp计算出在每个位置(i, j)上下左右最多有多少个连续的1,得到up[i][j], down[i][j], left[i][j], right[i][j].然后计算这四个值中的最小值 ...

  3. Angular jsonp 同源策略的问题

    引用:http://www.cnblogs.com/dengzy/p/5388357.html 说到AJAX就会不可避免的面临两个问题,第一个是AJAX以何种格式来交换数据?第二个是跨域的需求如何解决 ...

  4. oracle DBA笔试题

    Unix/Linux题目: 1.如何查看主机CPU.内存.IP和磁盘空间? cat /proc/cpuinfo cat /proc/meminfo ifconfig –a fdisk –l   2.你 ...

  5. mybatis-paginator对SqlServer分页实现

    package com.github.miemiedev.mybatis.paginator.dialect; import com.github.miemiedev.mybatis.paginato ...

  6. HTML标签的分类

    html中的标签元素大体被分为三种不同的类型:块状元素.内联元素和内联块状元素.常用的块状元素有:<div>.<p>.<h1>...<h6>.<o ...

  7. c++通过管道pipe获取cmd输出的字符

    #include <stdio.h>#include<iostream>#include<string>using namespace std; // 描述:exe ...

  8. Discuz 页面不能加载插件的原因和解决方法

    模板中,<!--{subtemplate common/headerF}-->这样就不能加载 source/class/class_template.php里65行附近代码 $header ...

  9. 【转】Web 前沿——HTML5 Form Data 对象的使用

    XMLHttpRequest Level 2 添加了一个新的接口——FormData.利用 FormData 对象,我们可以通过 JavaScript 用一些键值对来模拟一系列表单控件,我们还可以使用 ...

  10. CAD参数绘制对齐标注(com接口)

    主要用到函数说明: _DMxDrawX::DrawDimAligned 绘制一个对齐标注.详细说明如下: 参数 说明 DOUBLE dExtLine1PointX 第一条界线开始点X值 DOUBLE ...