Maximum Product Subarray - Complete Solution Guide
Maximum Product Subarray is LeetCode problem 152, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given an integer array nums , find a subarray that has the largest product, and return the product . The test cases are generated so that the answer will fit in a 32-bit integer. 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 * 10 4 -10 <= nums[i] <= 10 The product of any subarray of nums is guaranteed
Detailed Explanation
The problem asks us to find the maximum product of any non-empty subarray within a given integer array `nums`. The key constraint is that the product of any subarray will fit within a 32-bit integer. This means we don't need to worry about integer overflow causing incorrect results. A subarray is a contiguous sequence of elements within the array. The goal is to find the subarray with the largest possible product.
Solution Approach
The solution uses dynamic programming principles to efficiently track the maximum and minimum products ending at each index of the array. For each element, it updates the current maximum and minimum product by considering the element itself, the element multiplied by the previous maximum product, and the element multiplied by the previous minimum product. The overall maximum product is then updated accordingly.
Step-by-Step Algorithm
- Step 1: Initialize `res`, `cur_max`, and `cur_min` to the first element of the array. `res` will store the overall maximum product, `cur_max` will store the maximum product ending at the current index, and `cur_min` will store the minimum product ending at the current index.
- Step 2: Iterate through the array from the second element (index 1).
- Step 3: For each element `num`, calculate the potential new maximum product: `max(num, num * cur_max, num * cur_min)`. This considers the case where the current number is greater than the product of the current number multiplied by the previous maximum or minimum.
- Step 4: Similarly, calculate the potential new minimum product: `min(num, num * cur_max, num * cur_min)`. The `cur_max` is used in calculation because multiplying a negative number by a larger number produces a smaller result (min).
- Step 5: Update `cur_max` and `cur_min` with the calculated maximum and minimum products. Importantly, we need to save the previous `cur_max` value before updating it since it's used in the `cur_min` calculation.
- Step 6: Update `res` with the maximum of `res` and `cur_max` to keep track of the overall maximum product encountered so far.
- Step 7: After iterating through the entire array, return `res`.
Key Insights
- Insight 1: The presence of negative numbers is crucial. Two negative numbers multiplied together result in a positive number, which could potentially lead to a larger product than any subarray containing only positive numbers.
- Insight 2: We need to keep track of both the maximum and minimum product ending at each index. The minimum product is important because multiplying it by a negative number could yield a new maximum product.
- Insight 3: Zeroes can reset the current product. If we encounter a zero, the current maximum and minimum products become 0. This effectively splits the array into separate subproblems.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Array, Dynamic Programming.
Companies
Asked at: Arcesium, DE Shaw, Goldman Sachs, Google, HashedIn, Hiver, LinkedIn, Nvidia, Oracle, PayPal, ServiceNow, TikTok, Wayfair, Yahoo, tcs.