Find the contiguous subarray within an array (containing at least one number) which have the largest product.
For example, given the array [2,3,-2,4]
,
The contiguous Subarray has the [2,3]
largest product = 6
.
Package com.liuhao.acm.leetcode;/** * @author Liuhao Find the contiguous subarray within a array (containing at * Lea St one number) which has the largest product. * For example, given the array [2,3,-2,4], the contiguous subarray * [2,3] have the largest product = 6. *
*/public class Maximumproductsubarray {public static int maxproduct (int[] A) {int len = a.length;if (len <= 0) {retur n 0;} if (len = = 1) {return a[0];} int max = A[0];int min = a[0];int result = a[0];for (int i = 1; i < Len; i++) {int max_temp = max * A[i];int min_temp = Min * A[i];max = Math.max (A[i], Math.max (max_temp, min_temp)); min = Math.min (A[i], math.min (max_temp, min_temp)); result = Math.max (result, max);} return result;} public static void Main (string[] args) {int[] a = new int[] { -4,-3}; System.out.println (Maxproduct (a));}}
"Leetcode Brush problem Java Edition" Maximum Product Subarray