Medium
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.
Example 1:
Input: nums = [2,3,-2,4]
Output: 6
Explanation: [2,3] has the largest product 6.
Example 2:
Input: nums = [-2,0,-1]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
Constraints:
1 <= nums.length <= 2 * 104-10 <= nums[i] <= 10- The product of any prefix or suffix of
numsis guaranteed to fit in a 32-bit integer.
To solve the problem of finding the maximum product subarray, you can use a dynamic programming approach to keep track of the maximum and minimum products at each position in the array. This approach works because the product of two negative numbers can be positive, and thus the minimum product can become the maximum if a negative number is encountered.
Here are the detailed steps and the corresponding implementation in the Solution class:
-
Initialization:
- Check if the input array
numsis empty. If it is, return 0. - Initialize three variables:
max_productto keep track of the maximum product found so far.current_maxto keep track of the maximum product ending at the current position.current_minto keep track of the minimum product ending at the current position.
- Check if the input array
-
Iterate through the Array:
- Loop through each element in the array starting from the first element.
- For each element, calculate the potential new values for
current_maxandcurrent_minconsidering the current element itself, the product ofcurrent_maxwith the current element, and the product ofcurrent_minwith the current element. - Update
current_maxto be the maximum of these values. - Update
current_minto be the minimum of these values. - Update
max_productto be the maximum ofmax_productandcurrent_max.
-
Return Result:
- After the loop,
max_productwill hold the maximum product of any subarray withinnums.
- After the loop,
class Solution:
def maxProduct(self, nums: List[int]) -> int:
if not nums:
return 0
max_product = nums[0]
current_max = nums[0]
current_min = nums[0]
for num in nums[1:]:
if num < 0:
current_max, current_min = current_min, current_max
current_max = max(num, current_max * num)
current_min = min(num, current_min * num)
max_product = max(max_product, current_max)
return max_product-
Initialization:
max_productis initialized to the first element of the array because the maximum product subarray must include at least one element.current_maxandcurrent_minare also initialized to the first element.
-
Iterate through the Array:
- For each element in
nums(starting from the second element):- If the current element is negative, swap
current_maxandcurrent_minbecause multiplying by a negative number flips the maximum and minimum. - Update
current_maxto be the maximum of the current element or the product ofcurrent_maxwith the current element. - Update
current_minto be the minimum of the current element or the product ofcurrent_minwith the current element. - Update
max_productto be the maximum ofmax_productandcurrent_max.
- If the current element is negative, swap
- For each element in
-
Return Result:
- The
max_productvariable now contains the maximum product of any contiguous subarray.
- The
This solution efficiently finds the maximum product subarray in O(n) time complexity with O(1) additional space complexity.