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. [SPOJ1811]Longest Common Substring 后缀自动机 最长公共子串

    题目链接:http://www.spoj.com/problems/LCS/ 题意如题目,求两个串的最大公共子串LCS. 首先对其中一个字符串A建立SAM,然后用另一个字符串B在上面跑. 用一个变量L ...

  2. Windows下使用JMeter

    简介 Apache JMeter是100%纯java桌面应用程序,被设计用来测试C/S结构的软件(例如web应用程序).它可以被用来测试包括基于静态和动态资源程序的性能,例如静态文件,Java Ser ...

  3. UI动画效果

    UI界面的动画效果总结 方式1:头尾式 //开始动画 [UIView beginAnimations:nil context:nil]; //设置动画时间 [UIView setAnimationDu ...

  4. QPushButton注册事件过滤器后按钮消失

    版权声明:本文为博主原创文章,转载需要注明出处. RT,代码如下: ui.btn_set->installEventFilter(this); bool MousrHoverTest::even ...

  5. vue2.0 动态切换组件

    组件标签是Vue框架自定义的标签,它的用途就是可以动态绑定我们的组件,根据数据的不同更换不同的组件. <!DOCTYPE html> <html lang="en" ...

  6. Mysql基本操作、C++Mysql简单应用、PythonMysql简单应用

    MySql基本操作 -- 当指定名称的数据库不存在时创建它并且指定使用的字符集和排序方式 CREATE DATABASE IF NOT EXISTS db_name CHARACTER SET UTF ...

  7. du查看文件大小

    du+文件名就可以查看文件大小 du+ -h + 文件名也是查看文件大小,只是-h会将文件大小转换成M,G等格式

  8. thinkphp5将一条数据以toArray()放入session

    直接将找出的数据赋予变量,列:$admin: 存入session:Session::set('user_info',$admin->toArray()); 如果数据中有时间字段: 如若有时间字段 ...

  9. iOS sandbox

    iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视频等内容.iOS应用产生的内容,如图像.文件.缓存内容等都必须存储在自己的沙盒内.默认 ...

  10. 06网络通信udp-tcp、正则

    一. udp网络程序 1.    udp网络程序-发送数据 1)创建客户端套接字 2)发送/接收数据 3)关闭套接字 from socket import * # 1. 创建udp套接字 udp_so ...