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. 2556. [NOIP2016]玩具谜题

    [题目描述] 小南有一套可爱的玩具小人,它们各有不同的职业.有一天,这些玩具小人把小南的眼镜藏了起来.小南发现玩具小人们围成了一个圈,它们有的面朝国内,有的面朝圈外.如下图: 这时singer告诉小南 ...

  2. vscode显示php函数列表

    1.安装插件支持 https://marketplace.visualstudio.com/items?itemName=linyang95.php-symbols 2.ctrt+shift+o 即可 ...

  3. 从0开始搭建SQL Server 2012 AlwaysOn 第一篇(AD域与DNS)

    随着业务发展,公司需要提高数据安全与性能需求,所以需要对新技术预研(先采坑),做技术积累: 了解相关AlwaysOn 故障转移集群(热备),数据路由(ICX),Moebius(莫比斯数据路由) 决定测 ...

  4. list map接口传递

    1.传参时可以设置为jsonArray的格式 JSONArray array = new JSONArray(); JSONObject json = new JSONObject(); json.p ...

  5. JS性能分析(测试代码运行时间)

    //性能优化 console.time("timer"); for(var i=0;i<10000;i++){} console.timeEnd("timer&qu ...

  6. 下载kaggle数据集,验证手机号

    https://blog.csdn.net/Tomxiaodai/article/details/80167765 kaggle上下载一下数据集必须手机验证,结果验证时一直提示错误输入的格式错误,试了 ...

  7. Linux环境下验证码不显示F12报500

     前言: 项目之前部署在linux系统上进行测试,今天重新部署的时候,重启了tomcat然后部署新的版本项目,结果登录页面验证码不显示,在浏览器F12页面显示的是500错误.网上查了很多方法,都没效果 ...

  8. 运用 node + express + http-proxy-middleware 实现前端代理跨域的 详细实例哦

    一.你需要准备的知识储备 运用node的包管理工具npm 安装插件.中间件的基本知识: 2.express框架的一些基础知识,知道如何建立一个小的服务器:晓得如何快速的搭建一个express框架小应用 ...

  9. asp.net:Parser Error & HTTP 错误 500.21 - Internal Server Error

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABEcAAAF8CAIAAACiwUIQAAAgAElEQVR4nO2dyZHzOpZGaVEaADNkBB ...

  10. 微信sdk 签名

    <?php namespace app\wechat\service; use think\Config; class Signature { protected $appId ; protec ...