lintcode 中等题 :Maximum Product Subarray 最大连续乘积子序列

题目

乘积最大子序列

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

样例

比如, 序列 [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

时间: 2024-08-25 06:46:26

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

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. 思路:类似最大和连续子序列那样,不过除了记录最大乘积,我们还要记录最小的乘积.这里我

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. 此题要求是要在一个无需的数组找出一个乘积最大的连续子数组 例如[2,3,-2,4],

【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

Add Date 2014-09-23 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 = 

leetcode_152题——Maximum Product Subarray(动态规划)

Maximum Product Subarray Total Accepted: 33022 Total Submissions: 170218My Submissions Question Solution 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

LeetCode 152. 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. 题目标签:Array, Dynamic Programming 题目给了我们一个nu

lintcode 中等题:continuous subarray sum 联系子数组之和

题目 连续子数组求和 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.(如果两个相同的答案,请返回其中任意一个) 样例 给定 [-3, 1, 3, -3, 4], 返回[1,4]. 解题 法一:直接暴力,时间复杂度O(N2),时间超时 public class Solution { /** * @param A an integer array * @return A list of integers includes the i

刷题152. Maximum Product Subarray

一.题目说明 题目152. Maximum Product Subarray,给一列整数,求最大连续子序列,其乘积最大.难度是Medium! 二.我的解答 这个题目,用双重循环就可以了. class Solution{ public: int maxProduct(vector<int>& nums){ if(nums.size()<=1) return nums[0]; product = INT_MIN; int len = nums.size(); for(int i=0;

LeetCode Maximum Product Subarray(枚举)

LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, you should determine what is the value of the maximum positive product involving consecutive terms of S. If you cannot find a positive sequence, you

LeetCode:Maximum Product Subarray

题目: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. 这道题属于动态规划的题型,