-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy path152. Maximum Product Subarray
More file actions
110 lines (98 loc) · 4.12 KB
/
152. Maximum Product Subarray
File metadata and controls
110 lines (98 loc) · 4.12 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
class Solution {
public int maxProduct(int[] nums) {
int numberConsecutiveNegatives = 0;
boolean repeat = false;
int globalMax = nums[0];
int interMaxProduct = 1;
if(nums.length == 1){
return nums[0];
}
int negativeNumber = 0;
for(int i = 0; i < nums.length; i++){
//Multiply by the next number
interMaxProduct *= nums[i];
//If bigger than globalMax, set globalMax = interMax
if(interMaxProduct > globalMax){
globalMax = interMaxProduct;
}
//If the number you multiplied by is 0, even if next number is negative, the subarray will have a 0 in it --> sum = 0
//Want to start over with the next value so you don't continuously multiply by 0
if(nums[i] == 0){
negativeNumber = 0;
interMaxProduct = 1;
if(numberConsecutiveNegatives % 2 == 1 && numberConsecutiveNegatives > 2){
repeat = true;
}
numberConsecutiveNegatives = 0;
}
//If you just multiplied by a negative number, see if there is a substring that's attachable that was negative (- * - = +)
//Else, keep track of this negative substring and start over and see if you will hit another negative
if(interMaxProduct < 0){
numberConsecutiveNegatives++;
if(negativeNumber != 0){
interMaxProduct *= negativeNumber;
negativeNumber = 0;
if(interMaxProduct > globalMax){
globalMax = interMaxProduct;
}
}
else{
negativeNumber = interMaxProduct;
interMaxProduct = 1;
}
}
}
if(numberConsecutiveNegatives % 2 == 1 && numberConsecutiveNegatives > 2){
repeat = true;
}
if(repeat){
//copy over numbers in reverse
int [] newArray = new int[nums.length];
for(int i = 0, length = nums.length; i < length; i++){
newArray[i] = nums[length - i - 1];
}
return Math.max(globalMax, maxProductHelper(newArray));
}
else{
return globalMax;
}
}
public int maxProductHelper(int [] nums){
int globalMax = nums[0];
int interMaxProduct = 1;
if(nums.length == 1){
return nums[0];
}
int negativeNumber = 0;
for(int i = 0; i < nums.length; i++){
//Multiply by the next number
interMaxProduct *= nums[i];
//If bigger than globalMax, set globalMax = interMax
if(interMaxProduct > globalMax){
globalMax = interMaxProduct;
}
//If the number you multiplied by is 0, even if next number is negative, the subarray will have a 0 in it --> sum = 0
//Want to start over with the next value so you don't continuously multiply by 0
if(nums[i] == 0){
negativeNumber = 0;
interMaxProduct = 1;
}
//If you just multiplied by a negative number, see if there is a substring that's attachable that was negative (- * - = +)
//Else, keep track of this negative substring and start over and see if you will hit another negative
if(interMaxProduct < 0){
if(negativeNumber != 0){
interMaxProduct *= negativeNumber;
negativeNumber = 0;
if(interMaxProduct > globalMax){
globalMax = interMaxProduct;
}
}
else{
negativeNumber = interMaxProduct;
interMaxProduct = 1;
}
}
}
return globalMax;
}
}