Leetcode 152. Maximum Product Subarray

January 1, 0001

152. Maximum Product Subarray

Result

Runtime: 233 ms, faster than 5.89% of Java online submissions for Maximum Product Subarray. Memory Usage: 42 MB, less than 29.04% of Java online submissions for Maximum Product Subarray.

class Solution {
    public int maxProduct(int[] nums) {
        int total = Integer.MIN_VALUE;
        int l = 0;
        int r = l;
        while(l < nums.length) { 
            int thisTotal = 1;
            while(r < nums.length) {
                thisTotal = thisTotal * nums[r];
                if(thisTotal > total) {
                    total = thisTotal;
                }
                r = r + 1;
            }
            l = l + 1;
            r = l;
        }
        return total;
    }
}