The Maximum Product Subarray problem is a classic algorithmic challenge often encountered in programming interviews and competitive coding. The objective is simple: given an array of integers (which can include both negative and positive values), the goal is to find the contiguous subarray that yields the maximum product.
Given an array nums, your task is to find the contiguous subarray within nums (containing at least one number) which has the largest product and return that product.
Example:
Input: [-2, 3, -4]
Output: 24
Explanation: The contiguous subarray [3, -4]
has the maximum product, which equals 3 * -4 = -12
, but taking into account the entire array results in a maximum product of -2 * 3 * -4 = 24
.
While it may be tempting to use a brute force approach to check every possible subarray, this would result in a time complexity of O(n^2). Instead, we need a more efficient solution, ideally O(n).
The key to tackling this problem lies in recognizing that the product of two negative numbers is positive. Thus, maintaining two values during our traversal—one for the maximum product and another for the minimum product—is crucial, as we can encounter a situation where a small negative number turns a large negative product into a larger positive product.
To implement a dynamic programming solution, we can use a single pass through the array to calculate the maximum and minimum product at each step.
Initialization:
Start with variables to track the maximum product (max_prod
), the minimum product (min_prod
), and the result (result
).
max_prod = min_prod = result = nums[0]
Iterate through the array:
For each number in the array from the second element onward:
max_prod
and min_prod
.max_prod
to be the maximum of the current number and the product of max_prod
with the current number.min_prod
in a similar manner.Return the result
After traversing the array, the resultant maximum product will hold the maximum product of any subarray.
Here’s the implementation in Python:
def max_product_subarray(nums): max_prod = min_prod = result = nums[0] for num in nums[1:]: if num < 0: max_prod, min_prod = min_prod, max_prod max_prod = max(num, max_prod * num) min_prod = min(num, min_prod * num) result = max(result, max_prod) return result
Let’s illustrate the operation of the algorithm using the example array [-2, 3, -4]
.
Initialization: max_prod = -2
, min_prod = -2
, result = -2
Iteration 1 (num = 3
):
3
is positive, so we continue.max_prod = max(3, -2 * 3) = 3
min_prod = min(3, -2 * 3) = -6
result = max(-2, 3) = 3
Iteration 2 (num = -4
):
-4
is negative, so swap max_prod
and min_prod
: now max_prod = -6
, min_prod = 3
max_prod = max(-4, -6 * -4) = 24
min_prod = min(-4, 3 * -4) = -12
result = max(3, 24) = 24
Finally, we return 24 as the maximum product subarray.
O(n)
- As we only traverse the array once.O(1)
- We are using a constant amount of additional space for our variables.With these techniques and insights, tackling the Maximum Product Subarray becomes a streamlined process, perfectly aligning with efficient algorithm design principles. As you practice more problems like this, the underlying patterns of dynamic programming will become increasingly clear and intuitive.
13/10/2024 | DSA
23/09/2024 | DSA
15/11/2024 | DSA
15/11/2024 | DSA
06/12/2024 | DSA
16/11/2024 | DSA
16/11/2024 | DSA
13/10/2024 | DSA
06/12/2024 | DSA
16/11/2024 | DSA
15/11/2024 | DSA