题目

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

样例

比如, 序列 [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连接问题

    问题一——Mysql number Error 2003 MySQL连接错误.错误代码10061.10061一般是Mysql服务没启动.或Mysql服务器无法连接 . 在程序栏找到Mysql\Mysq ...

  2. python杂记-6(time&datetime模块)

    #!/usr/bin/env python# -*- coding: utf-8 -*-import timeprint(time.clock())##返回处理器时间,3.3开始已废弃 , 改成了ti ...

  3. impdp ORA-29913: error in executing ODCIEXTTABLEOPEN callout

    1.数据导出时的日志 ;;; Export: Release :: Copyright (c) , , Oracle and/or its affiliates. All rights reserve ...

  4. Python脚本控制的WebDriver 常用操作 <二十> 处理表单元素

    测试用例场景 表单对象的操作比较简单,只需要记住下面几点 使用send_keys方法往多行文本框和单行文本框赋值: 使用click方法选择checkbox 使用click方法选择radio 使用cli ...

  5. SVM入门

    前言: 又有很长的一段时间没有更新博客了,距离上次更新已经有两个月的时间了.其中一个很大的原因是,不知道写什么好-_-,最近一段时间看了看关于SVM(Support Vector Machine)的文 ...

  6. 转:一份基础的嵌入式Linux工程师笔试题

    一. 填空题: 1. 一些Linux命令,显示文件,拷贝,删除 Ls cp rm 2. do……while和while……do有什么区别? 3. Linux系统下.ko文件是什么文件?.so文件是什么 ...

  7. (转) java 复制文件,不使用输出流复制,高效率,文件通道的方式复制文件

    public static void fileChannelCopy(File s, File t) { FileInputStream fi = null; FileOutputStream fo ...

  8. 【狼窝乀野狼】Excel那些事儿

    在工作中我们常常遇到Excel表格,不管是数据的导入导出,还是财务统计什么都,都离不开Excel,Excel是我见过的最牛逼的一个软件(可能我的见识少)没有之一:如果你只停留在Excel处理数据,统计 ...

  9. android开发 更新升级安装到一半自动闪退

    如题:android开发 更新升级安装到一半自动闪退,,,解决办法,如下(红色为我新增的代码) /**     * 安装APK文件     */    private void installApk( ...

  10. Ubuntu 查看文件以及磁盘空间大小命令df

    (1)查看文件大小 查看当前文件夹下所有文件大小(包括子文件夹)    du -sh   # du -h 15M     ./package 16K     ./.fontconfig 4.0K    ...