题目

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

样例

比如, 序列 [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. MySql安装时在Start Service处失败

    安装MySql时在最后Start Service时失败: 首先先卸载掉MySql. 查看MySql服务有没有启动,若启动,则先停止MySql服务.   打开注册表,到HKEY_LOCAL_MACHIN ...

  2. TCP协议承载的DNS报文,DNS报文首部前多出两个字节的DNS报文长度字段,是何意义?

    一.TCP报文头部简介 ●源.目标端口号字段:占16比特.TCP协议通过使用"端口"来标识源端和目标端的应用进程.端口号可以使用0到65535之间的任何数字.在收到服务请求时,操作 ...

  3. C# 解析XML格式的字符串

    public CreateOrderReturnResult GetCreateOrderReturnApi() { var result = new CreateOrderReturnResult( ...

  4. 关于SQLite的创建以及使用相关说明

    关于SQLite的创建以及使用相关说明 没有给出具体的程序,但看完这后可能对你有所帮助. 数据库操作基本知识: execSQL(String sql): 执行一个数据库语句 insert(table, ...

  5. EventHandler委托的使用

    今天复习了一下事件和委托,本来看事件来着,看到EventHandler,写了一个小例子,想贴在这里解释一下.为了弄清楚EventHandler, 还是回归到最基本的委托,曾经在园子里看到一位前辈用深入 ...

  6. oracle 11g 通过透明网关链接mysql

    之前转载过一篇在Windows上安装的,自己实际在centos上安装了一下.以下为安装记录: 一.操作系统环境 二.数据库环境(用oracle用户登录) 三.DG4ODBC 在Oracle DB 11 ...

  7. 第一个C#应用 【搜索软件】

    搜索软件V1.0 [附软件截图][http://pan.baidu.com/s/1mihEbe4] 设备搜索:支持广播搜索[local search],指定ip[range search]搜索,直接w ...

  8. Error:/etc/fstab:Read-only file system错误的解决办法

    1.挂载60T存储,设置开机自动挂载,UUID编号配置错误导致系统无法启动 2.根据提示进入维护状态,输入root密码,进入fstab删除UUID等内容,结果报错     Error:/etc/fst ...

  9. Window.Open参数、返回值

    一.window.open()支持环境: JavaScript1.0+/JScript1.0+/Nav2+/IE3+/Opera3+ 二.基本语法: window.open(pageURL,name, ...

  10. firefox常用扩展、脚本

    1.AutoPopup.uc.js:鼠标移到菜单和下拉箭头上自动弹出下拉菜单 2.moveButton.uc.js:移动或克隆按钮或菜单到火狐浏览器的任意位置 moveButton.uc.js使用说明 ...