Maximum Product Subarray

public class Solution {
    public int maxProduct(int[] A) {
        int res = A[0]; // max is local max, res is global max
        int tmax = res, tmin =res, max = res, min = res;
        for(int i=1; i< A.length; i++){
            tmax = max;
            tmin = min;
            max = Math.max(Math.max(tmax*A[i], tmin*A[i]), A[i]);
            min = Math.min(Math.min(tmax*A[i], tmin*A[i]), A[i]);
            res = Math.max(res,max);
        }
        return res;
    }
}

跟 jump game什么的一样

posted @ 2015-04-14 11:31  世界到处都是小星星  阅读(96)  评论(0编辑  收藏  举报