JAVA算法:求最大连续子序列乘积问题(JAVA版本)

JAVA算法:求最大子数组乘积问题(JAVA版本)

给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数)。

问题分析

 

算法设计

Solution 1

package com.bean.algorithmbasic;

public class MaximumProductSubarray {
	
	/*
	 * 最大连续子数组乘积
	 * 
	 * 给定一个整形数组,求最大子数组的乘积。
	 * 
	 * 问题分析:
	 * 数组元素可能包含正数、负数和0.
	 * 另外,根据题目要求,子数组要连续。
	 * 
	 * */
	
	public static int maxProduct(int[] nums) {
		//如果数组为null或者数组长度为0,直接返回0.
        if (nums == null || nums.length == 0) {
            return 0;
        }
        
        //开始将A[0]分别设置为:最大值、最小值,和最终的返回结果。
        int max = nums[0], min = nums[0], result = nums[0];
        //从数组的第二个元素开始取值
        for (int i = 1; i < nums.length; i++) {
            int temp = max;
            
            //状态转换方程的定义和实现
            max = Math.max(Math.max(max * nums[i], min * nums[i]), nums[i]);
            min = Math.min(Math.min(temp * nums[i], min * nums[i]), nums[i]);
            
            //始终用result存放最大值,以返回结果。
            if (max > result) {
                result = max;
            }
        }
        return result;
    }
	
	

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] arraydemo= {2,5,-1,2,-1};
		int RESULT=maxProduct(arraydemo);
		System.out.println("RESULT IS: "+RESULT);
	}

}

程序运行结果:

RESULT IS: 20


Solution 2

package com.bean.algorithmbasic;

public class MaximumProductSubarray2 {
	
	/*
	 * 最大连续子数组乘积
	 * 
	 * 给定一个整形数组,求最大子数组的乘积。
	 * 
	 * 问题分析:
	 * 数组元素可能包含正数、负数和0.
	 * 另外,根据题目要求,子数组要连续。
	 * 
	 * */
	
	public static int maxProduct(int[] a) {
		//如果数组为null或者数组长度为0,直接返回0.
		if (a == null || a.length == 0)
		    return 0;
		 //开始将A[0]分别设置为:最大值、最小值,和最终的返回结果。
		int result = a[0], min = result, max = result;
		  
		for (int i = 1; i < a.length; i++) {
			//如果a[i]>=0,则最大值在a[i]和 max*a[i]之间选取最大的一个结果
		    if (a[i] >= 0) {
		      max = Math.max(a[i], max * a[i]);
		      min = Math.min(a[i], min * a[i]);
		    } else {
		      int tmp = max;
		      max = Math.max(a[i], min * a[i]);
		      min = Math.min(a[i], tmp * a[i]);
		    }
		    
		    result = Math.max(result, max);
		}
		 
		return result;
    }
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] arraydemo= {2,5,-1,2,-1};
		int RESULT=maxProduct(arraydemo);
		System.out.println("RESULT IS: "+RESULT);
	}

}

程序运行结果

RESULT IS: 20


Solution 3

package com.bean.algorithmbasic;

public class MaximumProductSubarray3 {
	
	/*
	 * 最大连续子数组乘积
	 * 
	 * 给定一个整形数组,求最大子数组的乘积。
	 * 
	 * 问题分析:
	 * 数组元素可能包含正数、负数和0.
	 * 另外,根据题目要求,子数组要连续。
	 * 
	 * */
	
	public static int maxProduct(int[] nums) {
        int n = nums.length;
        int maxPro = Math.max(nums[0], nums[n - 1]);
        int[] dp = new int[n];
        
        // 从左至右
        dp[0] = nums[0];
        for (int i = 1; i < n; i++) {
            if (nums[i - 1] == 0) dp[i] = nums[i];
            else dp[i] = nums[i] * dp[i - 1];
            maxPro = Math.max(maxPro, dp[i]);
        }
        
        // 从右至左
        dp[n - 1] = nums[n - 1];
        for (int i = n - 2; i >= 0; i--) {
            if (nums[i + 1] == 0) dp[i] = nums[i];
            else dp[i] = nums[i] * dp[i + 1];
            maxPro = Math.max(maxPro, dp[i]);
        }
        return maxPro;
    }
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//int[] arraydemo= {2,3,-2,4};
		//int[] arraydemo= {-3,0,-2,4,5};
		//int[] arraydemo= {1,-1,2,3,5,-2,4};
		// int[] arraydemo= {-1,-1,-1};
		int[] arraydemo= {2,5,-1,2,-1};
		int RESULT=maxProduct(arraydemo);
		System.out.println("RESULT IS: "+RESULT);
	}

}

程序运行结果:

RESULT IS: 20

 

你可能感兴趣的:(算法分析与设计,JAVA算法学习)