Skip to content

Commit 828560e

Browse files
authored
Create 0152-Maximum-Product-Subarray.md
1 parent 7549525 commit 828560e

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
---
2+
id: maximum-product-subarray
3+
title: Maximum Product Subarray
4+
sidebar_label: 0152 Maximum Product Subarray
5+
tags:
6+
- Array
7+
- Dynamic Programming
8+
- LeetCode
9+
- Python
10+
- Java
11+
- C++
12+
description: "This is a solution to the Maximum Product Subarray problem on LeetCode."
13+
---
14+
15+
## Problem Description
16+
17+
Given an integer array nums, find a subarray that has the largest product, and return the product.
18+
19+
The test cases are generated so that the answer will fit in a 32-bit integer.
20+
21+
### Examples
22+
23+
**Example 1:**
24+
25+
```
26+
Input: nums = [2,3,-2,4]
27+
Output: 6
28+
Explanation: [2,3] has the largest product 6.
29+
```
30+
31+
**Example 2:**
32+
33+
```
34+
Input: nums = [-2,0,-1]
35+
Output: 0
36+
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
37+
```
38+
39+
### Constraints
40+
41+
- $1 <= nums.length <= 2 * 10**4$
42+
- $10 <= nums[i] <= 10$
43+
- $The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.$
44+
45+
46+
#### Code in Different Languages
47+
48+
<Tabs>
49+
<TabItem value="Python" label="Python">
50+
<SolutionAuthor name="@mahek0620"/>
51+
```python
52+
class Solution(object):
53+
def maxProduct(self, nums):
54+
"""
55+
:type nums: List[int]
56+
:rtype: int
57+
"""
58+
if not nums:
59+
return 0
60+
61+
max_prod = nums[0]
62+
min_prod = nums[0]
63+
result = max_prod
64+
65+
for num in nums[1:]:
66+
if num < 0:
67+
max_prod, min_prod = min_prod, max_prod
68+
69+
max_prod = max(num, max_prod * num)
70+
min_prod = min(num, min_prod * num)
71+
72+
result = max(result, max_prod)
73+
74+
return result
75+
76+
77+
78+
```
79+
80+
</TabItem>
81+
<TabItem value="Java" label="Java">
82+
<SolutionAuthor name="@mahek0620"/>
83+
```java
84+
class Solution {
85+
public int maxProduct(int[] nums) {
86+
if (nums.length == 0)
87+
return 0;
88+
89+
int maxProd = nums[0];
90+
int minProd = nums[0];
91+
int result = maxProd;
92+
93+
for (int i = 1; i < nums.length; ++i) {
94+
if (nums[i] < 0) {
95+
int temp = maxProd;
96+
maxProd = minProd;
97+
minProd = temp;
98+
}
99+
100+
maxProd = Math.max(nums[i], maxProd * nums[i]);
101+
minProd = Math.min(nums[i], minProd * nums[i]);
102+
103+
result = Math.max(result, maxProd);
104+
}
105+
106+
return result;
107+
}
108+
}
109+
110+
111+
```
112+
113+
</TabItem>
114+
<TabItem value="C++" label="C++">
115+
<SolutionAuthor name="@mahek0620"/>
116+
```cpp
117+
#include <vector>
118+
#include <algorithm>
119+
120+
class Solution {
121+
public:
122+
int maxProduct(vector<int>& nums) {
123+
if (nums.empty())
124+
return 0;
125+
126+
int max_prod = nums[0];
127+
int min_prod = nums[0];
128+
int result = max_prod;
129+
130+
for (int i = 1; i < nums.size(); ++i) {
131+
if (nums[i] < 0)
132+
std::swap(max_prod, min_prod);
133+
134+
max_prod = std::max(nums[i], max_prod * nums[i]);
135+
min_prod = std::min(nums[i], min_prod * nums[i]);
136+
137+
result = std::max(result, max_prod);
138+
}
139+
140+
return result;
141+
}
142+
};
143+
144+
145+
```
146+
147+
</TabItem>
148+
</Tabs>
149+
150+
151+
</TabItem>
152+
153+
</Tabs>
154+
155+
## References
156+
157+
- **LeetCode Problem**: [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/)
158+
159+
- **Solution Link**: [LeetCode Solution](https://leetcode.com/problems/maximum-product-subarray/solution/)
160+
161+
- **Authors GeeksforGeeks Profile:** [Mahek Patel](https://leetcode.com/u/mahekrpatel611/)

0 commit comments

Comments
 (0)