[leetcode] Maximum Product Subarray

来源:互联网 发布:中国电信cn2网络 编辑:程序博客网 时间:2024/05/01 11:42
<span style="white-space:pre"></span>public int maxProduct(int[] A, int start, int end) {            int product = 1;            int leftProduct = 1;            for(int i = start; i <= end; i++)            {            product *= A[i];            }            int max = product;            for(int i = start; i < end; i++)            {            leftProduct *=A[i];            int rightProduct = product/leftProduct;            max = Math.max(leftProduct, max);            max = Math.max(rightProduct, max);            }            return max;        }        public int maxProduct(int A[])    {    int max = Integer.MIN_VALUE;    int start = 0;    for(int i = 0; i < A.length; i++)    {    if(A[i] == 0)    {    if(start <= i-1)    {    max = Math.max(max, maxProduct(A, start, i-1));    }    max = Math.max(0, max);    start = i+1;    }    }    if(start < A.length)        max = Math.max(max, maxProduct(A, start, A.length-1));    return max;    }

0 0
原创粉丝点击