Skip to content

Latest commit

 

History

History
21 lines (20 loc) · 478 Bytes

152. Maximum Product Subarray.md

File metadata and controls

21 lines (20 loc) · 478 Bytes

152. Maximum Product Subarray

Solution 1

  • time: O(n*n), using two for loops
class Solution {
    public int maxProduct(int[] nums) {
        //O(n*n)
        if(nums.length==0) return 0;
        int max = nums[0];
        for(int i = 0; i<nums.length; i++){
            int mul = 1;
            for(int j = i; j<nums.length; j++){
                mul *= nums[j];
                max = Math.max(mul, max);
            }
        }
        return max;
    }
}