LintCode -- 乘积最大子序列

LintCode -- maximum-product-subarray(乘积最大子序列)


原题链接:http://www.lintcode.com/zh-cn/problem/maximum-product-subarray/


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

样例

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


分析:

遍历数组,并记录 i - 1个数的时候的最大值和最小值。针对第 i 个数的正负来选择更新最大值和最小值的方法。

****   时间复杂度 O(n), 空间复杂度 O(1) ****


代码(C++、Java、Python):

class Solution {
public:
    /**
     * @param nums: a vector of integers
     * @return: an integer
     */
    int min(int a, int b){
        if (a < b) return a;
        else return b;
    }
    int max(int a, int b){
        if (a > b) return a;
        else return b;
    }
    int maxProduct(vector& nums) {
        // write your code here
        int n = nums.size();
        if (n == 1) return nums[0];
        int res = nums[0];
        int minlist[2] = {nums[0]};
        int maxlist[2] = {nums[0]};
        for (int i = 1; i < n; i++){
            minlist[i%2] = nums[i];
            maxlist[i%2] = nums[i];
            if (nums[i] > 0){
                maxlist[i%2] = max(maxlist[i%2], maxlist[(i-1)%2]*nums[i]);
                minlist[i%2] = min(minlist[i%2], minlist[(i-1)%2]*nums[i]);
            }
            else {
                maxlist[i%2] = max(maxlist[i%2], minlist[(i-1)%2]*nums[i]);
                minlist[i%2] = min(minlist[i%2], maxlist[(i-1)%2]*nums[i]);
            }
            res = max(res, maxlist[i%2]);
        }
        return res;
    }
};

public class Solution {
    /**
     * @param nums: an array of integers
     * @return: an integer
     */
    public int maxProduct(int[] nums) {
        // write your code here
        int n = nums.length;
        if (n == 1) return nums[0];
        int res = nums[0];
        int [] minlist = new int [2];
        int [] maxlist = new int [2];
        for (int i = 0; i < 2; i++){
            minlist[i] = nums[0];
            maxlist[i] = nums[0];
        }
        for (int i = 1; i < n; i++){
            minlist[i%2] = nums[i];
            maxlist[i%2] = nums[i];
            if (nums[i] > 0){
                maxlist[i%2] = max(maxlist[i%2], maxlist[(i-1)%2]*nums[i]);
                minlist[i%2] = min(minlist[i%2], minlist[(i-1)%2]*nums[i]);
            }
            else {
                maxlist[i%2] = max(maxlist[i%2], minlist[(i-1)%2]*nums[i]);
                minlist[i%2] = min(minlist[i%2], maxlist[(i-1)%2]*nums[i]);
            }
            res = max(res, maxlist[i%2]);
        }
        return res;
    }
    int min(int a, int b){
        if (a < b) return a;
        else return b;
    }
    int max(int a, int b){
        if (a > b) return a;
        else return b;
    }
}

class Solution:
    # @param nums: an integer[]
    # @return: an integer
    def maxProduct(self, nums):
        # write your code here
        n = len(nums)
        if n == 1:
            return nums[0]
        res = nums[0]
        minlist = [nums[0] for i in range(2)]
        maxlist = [nums[0] for i in range(2)]
        for i in range(1, n):
            minlist[i%2] = nums[i]
            maxlist[i%2] = nums[i]
            if nums[i] > 0:
                maxlist[i%2] = max(maxlist[i%2], maxlist[(i-1)%2]*nums[i])
                minlist[i%2] = min(minlist[i%2], minlist[(i-1)%2]*nums[i])
            else:
                maxlist[i%2] = max(minlist[i%2], minlist[(i-1)%2]*nums[i])
                minlist[i%2] = min(minlist[i%2], maxlist[(i-1)%2]*nums[i])
            res = max(res, maxlist[i%2])
        return res

def max(a, b):
    if a > b:
        return a
    else:
        return b

def min(a, b):
    if a < b:
        return a
    else:
        return b



你可能感兴趣的:(LintCode)