-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximum Product Subarray
More file actions
39 lines (32 loc) · 869 Bytes
/
Copy pathMaximum Product Subarray
File metadata and controls
39 lines (32 loc) · 869 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
30
31
32
33
34
35
36
37
38
39
LEETCODE PROBLEM SOLVING
PROBLEM->MAXIMUM PRODUCT SUBARRAY
class Solution {
public int maxProduct(int[] nums) {
int maxProduct = nums[0];
int currentMax = nums[0];
int currentMin = nums[0];
for (int i = 1; i < nums.length; i++) {
int num = nums[i];
int tempMax = Math.max(
num,
Math.max(
num * currentMax,
num * currentMin
)
);
currentMin = Math.min(
num,
Math.min(
num * currentMax,
num * currentMin
)
);
currentMax = tempMax;
maxProduct = Math.max(
maxProduct,
currentMax
);
}
return maxProduct;
}
}