DEV Community

codingpineapple
codingpineapple

Posted on

LeetCode 152. Maximum Product Subarray (javascript solution)

Description:

Given an integer array nums, find a contiguous non-empty subarray within the array that has the largest product, and return the product.

It is guaranteed that the answer will fit in a 32-bit integer.

A subarray is a contiguous subsequence of the array.

Solution:

Time Complexity : O(n)
Space Complexity: O(1)

var maxProduct = function(nums) {
    // Max product in the current contiguous array
    let currentMax = nums[0];
    // Mind product in the current contiguous array. We need this value in case we encounter 2 negative numbers whose product could potentially give us the max product of the entire array
    let currentMin = nums[0];
    // Max product of a contiguous array
    let finalMax = nums[0];

  for(let i = 1; i < nums.length; i++){
    let temp = currentMax
    // Because we are looking for a contiguous subarray product, the current max must contain the current number in the array. 
    currentMax = Math.max(Math.max(currentMax * nums[i], currentMin*nums[i]), nums[i])
    // Use temp here in case the previous currentMax was negative
    currentMin = Math.min(Math.min(temp * nums[i], currentMin*nums[i]), nums[i])
    // Record highest max at the end of every contiguous subarray
    finalMax = Math.max(currentMax, finalMax);
  }

  return finalMax;
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)