Maximum Product Subarray(最大连续乘积子序列)
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(最大连续乘积子序列)的更多相关文章
- lintcode 中等题 :Maximum Product Subarray 最大连续乘积子序列
题目 乘积最大子序列 找出一个序列中乘积最大的连续子序列(至少包含一个数). 样例 比如, 序列 [2,3,-2,4] 中乘积最大的子序列为 [2,3] ,其乘积为6. 解题 法一:直接暴力求解 时 ...
- Maximum Product Subarray 最大连续乘积子集
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- LeetCode 152. Maximum Product Subarray (最大乘积子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 46.Maximum Product Subarray(最大乘积子数组)
Level: Medium 题目描述: Given an integer array nums, find the contiguous subarray within an array (con ...
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关
Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...
- LeetCode Maximum Product Subarray(枚举)
LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...
- LeetCode_Maximum Subarray | Maximum Product Subarray
Maximum Subarray 一.题目描写叙述 就是求一个数组的最大子序列 二.思路及代码 首先我们想到暴力破解 public class Solution { public int maxSub ...
随机推荐
- html5的表单元素总结
- 安装JPype时出现的 Unable to find vcvarsall.bat
解决方案,在网上找到的,mark一下,亲测有效 C:/Python31/Lib/distutils目录下的msvc9compiler.py中 修改MSVCCompiler函数:vc_env = que ...
- WebSocket 的一些简单页面推送使用
因为做通信项目的时候,需要实时获取每个分机的当前状态,发现websocket还不错,只是对浏览器的要求比较高, 针对特定用户推送消息,网上有一些 public class GetHttpSession ...
- 大写URL转小写
添加LowercaseRoutesMVC.dll引用.通过“管理—NuGet程序包”搜索LowercaseRoutesMVC,然后点击安装.安装成功后会自动引用LowercaseRoutesMVC.d ...
- swift派发机制的核心是确定一个函数能否进入动态派发列表
swift派发机制的核心是确定一个函数能否进入动态派发列表
- CAD绘制固定矩形标注(网页版)
js中实现代码说明: function DoFixRectComment() { var ent = mxOcx.DrawCustomEntity("TestMxCustomEntity&q ...
- js 判断访问终端类型
// 判断访问终端类型 var browser = { versions: function() { var u = navigator.userAgent, app = navigator.appV ...
- HTML head meta标签详细
<!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --> <html lang="zh-cmn-Hans"&g ...
- java_StringBuffer、StringBuilder
StringBuffer和StringBuider是可变的字符串,使用方法 相同,StringBuffer是线程安全的,StringBuider是线程不安全的 public class StringT ...
- python 读取文件生成嵌套列表
def read_data(file_name): if not re.findall(".txt", file_name): file_name += ".txt&qu ...