-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay68.java
More file actions
27 lines (22 loc) · 868 Bytes
/
Day68.java
File metadata and controls
27 lines (22 loc) · 868 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
public class Day68 {
public int maxProduct(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int maxProd = nums[0];
int minProd = nums[0];
int result = nums[0];
for (int i = 1; i < nums.length; i++) {
int temp = maxProd;
maxProd = Math.max(Math.max(maxProd * nums[i], minProd * nums[i]), nums[i]);
minProd = Math.min(Math.min(temp * nums[i], minProd * nums[i]), nums[i]);
result = Math.max(result, maxProd);
}
return result;
}
public static void main(String[] args) {
MaximumProductSubarray obj = new MaximumProductSubarray();
int[] arr = {-3, 4, 5};
System.out.println("Maximum product of contiguous subarray: " + obj.maxProduct(arr)); // Output: 20
}
}