题目

找出一个序列中乘积最大的连续子序列(至少包含一个数)。

样例

比如, 序列 [2,3,-2,4] 中乘积最大的子序列为 [2,3] ,其乘积为6

解题 

法一:直接暴力求解

时间复杂度O(N2)

public class Solution {
/**
* @param nums: an array of integers
* @return: an integer
*/
public int maxProduct(int[] nums) {
// write your code here
if(nums == null)
return 0;
int MAX = Integer.MIN_VALUE;
int n = nums.length;
for(int i=0;i<= n-1 ;i++){
int pro = 1;
for(int j = i;j< n;j++){
pro *= nums[j];
MAX = Math.max(MAX,pro);
}
}
return MAX;
}
}

Java Code

总耗时: 2412 ms

法二:利用动态规划

个人感觉不好写,这里的数组有整数也有负数,某子数组乘积最大,有两种情况:1,负数*负数,2,正数*正数,所以要考虑两种情况,我只用第二种求解时候,发现了问题,毕竟许多负数成绩时候也可能取得整数的。

负数当然要选取最小的负数了,正数要是最大的正数。

maxLocal = A[0]

minLocal = A[0]

global = A[0]

在变量A数组的过程中:

maxLocal = max(maxLocal*A[i],A[i],minLocal*A[i])

minLocal = min(maxLocal*A[i],A[i],minLocal*A[i])

global = max(maxLocal,global)

上面中间的A[i],是可能断了的情况,与之前求最大/小子数组的和是一个道理《乘以、加,减一个数,我们要选取我们需要的数咯》

public class Solution {
/**
* @param nums: an array of integers
* @return: an integer
*/
public int maxProduct(int[] A) {
// write your code here
if(A == null || A.length ==0)
return 0;
int maxLocal = A[0];
int minLocal = A[0];
int global = A[0];
for(int i=1;i< A.length; i++){
int tmp = maxLocal;
maxLocal = Math.max(Math.max(A[i]*maxLocal,A[i]),
A[i]*minLocal);
minLocal = Math.min(Math.min(A[i]*tmp,A[i]),
A[i]*minLocal);
global = Math.max(maxLocal,global);
}
return global; }
}

Java Code

总耗时: 1823 ms

class Solution:
# @param nums: an integer[]
# @return: an integer
def maxProduct(self, nums):
# write your code here
if nums == None or len(nums) ==0:
return 0
maxLocal = nums[0]
minLocal = nums[0]
Global = nums[0]
for i in range(1,len(nums)):
num = nums[i]
tmp = maxLocal
maxLocal = max(max(num*maxLocal,num),num*minLocal)
minLocal = min(min(num*tmp,num),num*minLocal)
Global = max(Global,maxLocal)
return Global

Python Code

总耗时: 376 ms

lintcode 中等题 :Maximum Product Subarray 最大连续乘积子序列的更多相关文章

  1. Maximum Product Subarray(最大连续乘积子序列)

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

  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】152 Maximum Product Subarray

    Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...

  8. LeetCode Maximum Product Subarray(枚举)

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

  9. 152. Maximum Product Subarray - LeetCode

    Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...

随机推荐

  1. js中隐式类型转换测试

    javascript数据类型: 使用typeof可以返回的数据类型有六种 "number" , "string" ,  "boolean" ...

  2. ios 控件

    反序列化 JSONModel 上拉刷新 下拉加载更多 MJRefresh AFNetworking 2.5 Asynchronous image downloader with cache - SDW ...

  3. hi,mongo!(1)

    用了很多年的关系型数据库,想换一种思路,学习一下最近比较火的mongo数据库. 一.下载.安装mongo 下载地址:http://www.mongodb.org/downloads(官网) 官网的mo ...

  4. Webserver issues | PHP manager for IIS

    4 down vote accepted In order to successfully install the PHP manager for IIS 8, you need the .NET 3 ...

  5. .NET SDK和下载

    http://blogs.msdn.com/b/dotnet/p/dotnet_sdks.aspx .NET SDK和下载 您可以通过下载.NET框架针对包和软件开发工具包,并使用它们与Visual ...

  6. c# 刻度:毫米 英寸 像素转换

    从目前所掌握的资料来看,c#程序中将毫米转换像素的方法无非两种: 第一种: 1: /// <summary> 2: /// 以毫米为单位的显示宽度 3: /// </summary& ...

  7. Oracle 表的连接方式(2)-----HASH JOIN的基本机制1

    我们对hash join的常见误解,一般包括两个: 第一个误解:是我们经常以为hash join需要对两个做join的表都做全表扫描 第二个误解:是经常以为hash join会选择比较小的表做buil ...

  8. Python学习笔记1——人人都爱列表

    一些BIF函数在列表中的应用: Python 3.3.4 (v3.3.4:7ff62415e426, Feb 10 2014, 18:13:51) [MSC v.1600 64 bit (AMD64) ...

  9. SQL2012 附加数据库提示5120错误解决方法

    在win8.1 x64系统上使用sql2012进行附加数据库(包括在x86系统正在使用的数据库文件,直接拷贝附加在X64系统中)时,提示无法打开文件,5120错误. 这个错误是因为没有操作权限,所以附 ...

  10. Ubuntu下Apache+php+mysql网站架设详解

    目录 1 基础 2 安装 2.1 安装LAMP 2.2 图形化管理软件(可选) 2.2.1 安装webmin 2.2.2 安装phpmyadmin 3 配置文件路径 3.1 常用命令 3.2 配置ap ...