-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumProductSubarray.java
More file actions
29 lines (29 loc) · 913 Bytes
/
Copy pathMaximumProductSubarray.java
File metadata and controls
29 lines (29 loc) · 913 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
28
29
public class MaximumProductSubarray{
public int maxProduct(int[] nums) {
if(nums.length == 1)
return nums[0];
int maxPos = 0;
int maxNeg = 0;
int max = 0;
for(int i = 0; i < nums.length; i++){
if(nums[i] == 0){
maxPos = maxNeg = 0;
} else if(nums[i] > 0){
maxPos = (maxPos > 1)? maxPos * nums[i] : nums[i];
maxNeg = maxNeg * nums[i];
} else {
int temp = (maxPos > 1)? maxPos * nums[i] : nums[i];
maxPos = maxNeg * nums[i];
maxNeg = temp;
}
if(max < maxPos)
max = maxPos;
}
return max;
}
public static void main(String[] argvs){
MaximumProductSubarray mps = new MaximumProductSubarray();
int[] a = {-4, -3};
mps.maxProduct(a);
}
}