152. Maximum Product Subarray LeetCode Solution
In this guide, you will get 152. Maximum Product Subarray LeetCode Solution with the best time and space complexity. The solution to Maximum Product Subarray problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum Product Subarray solution in C++
- Maximum Product Subarray solution in Java
- Maximum Product Subarray solution in Python
- Additional Resources
Problem Statement of Maximum Product Subarray
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 * 104
-10 <= nums[i] <= 10
The product of any subarray of nums is guaranteed to fit in a 32-bit integer.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
152. Maximum Product Subarray LeetCode Solution in C++
class Solution {
public:
int maxProduct(vector<int>& nums) {
int ans = nums[0];
int dpMin = nums[0]; // the minimum so far
int dpMax = nums[0]; // the maximum so far
for (int i = 1; i < nums.size(); ++i) {
const int num = nums[i];
const int prevMin = dpMin; // dpMin[i - 1]
const int prevMax = dpMax; // dpMax[i - 1]
if (num < 0) {
dpMin = min(prevMax * num, num);
dpMax = max(prevMin * num, num);
} else {
dpMin = min(prevMin * num, num);
dpMax = max(prevMax * num, num);
}
ans = max(ans, dpMax);
}
return ans;
}
};
/* code provided by PROGIEZ */
152. Maximum Product Subarray LeetCode Solution in Java
class Solution {
public int maxProduct(int[] nums) {
int ans = nums[0];
int dpMin = nums[0]; // the minimum so far
int dpMax = nums[0]; // the maximum so far
for (int i = 1; i < nums.length; ++i) {
final int num = nums[i];
final int prevMin = dpMin; // dpMin[i - 1]
final int prevMax = dpMax; // dpMax[i - 1]
if (num < 0) {
dpMin = Math.min(prevMax * num, num);
dpMax = Math.max(prevMin * num, num);
} else {
dpMin = Math.min(prevMin * num, num);
dpMax = Math.max(prevMax * num, num);
}
ans = Math.max(ans, dpMax);
}
return ans;
}
}
// code provided by PROGIEZ
152. Maximum Product Subarray LeetCode Solution in Python
class Solution:
def maxProduct(self, nums: list[int]) -> int:
ans = nums[0]
dpMin = nums[0] # the minimum so far
dpMax = nums[0] # the maximum so far
for i in range(1, len(nums)):
num = nums[i]
prevMin = dpMin # dpMin[i - 1]
prevMax = dpMax # dpMax[i - 1]
if num < 0:
dpMin = min(prevMax * num, num)
dpMax = max(prevMin * num, num)
else:
dpMin = min(prevMin * num, num)
dpMax = max(prevMax * num, num)
ans = max(ans, dpMax)
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.