-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy path152._Maximum_Product_Subarray.java
More file actions
27 lines (22 loc) · 919 Bytes
/
152._Maximum_Product_Subarray.java
File metadata and controls
27 lines (22 loc) · 919 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Problem link
// https://leetcode.com/problems/maximum-product-subarray/
// Basically to undertand this solution, one should have idea on Kadanes Algorithm which is a DP approach to solve Maximum Subarray Sum.
// This question just replaced sum with product and we know that we got headache with the negative values if you have understood the question properly.
// So I swapped min and max values at current iteration of negative value.
class Solution {
public int maxProduct(int[] nums) {
int max=nums[0], min=nums[0],prod=max;
for(int i=1;i<nums.length;i++){
if(nums[i]<0){
//swap max and min
int temp=max;
max=min;
min=temp;
}
max=Math.max(nums[i],nums[i]*max);
min=Math.min(nums[i],nums[i]*min);
if(prod<max) prod=max;
}
return prod;
}
}