Skip to content

Commit cf7f973

Browse files
committed
fix-lints
1 parent 6702fd0 commit cf7f973

File tree

1 file changed

+6
-27
lines changed

1 file changed

+6
-27
lines changed

src/main/java/com/thealgorithms/dynamicprogramming/MaximumProductSubarray.java

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
* large arrays.</p>
1616
*/
1717
public final class MaximumProductSubarray {
18+
1819
private MaximumProductSubarray() {
20+
// Prevent instantiation
1921
}
2022

2123
/**
@@ -24,7 +26,7 @@ private MaximumProductSubarray() {
2426
* @param nums an array of integers which may contain positive, negative,
2527
* and zero values.
2628
* @return the maximum product of a contiguous subarray. Returns 0 if the
27-
* array is empty.
29+
* array is null or empty.
2830
*/
2931
public static int maxProduct(int[] nums) {
3032
if (nums == null || nums.length == 0) {
@@ -36,44 +38,21 @@ public static int maxProduct(int[] nums) {
3638
int currentMin = nums[0];
3739

3840
for (int i = 1; i < nums.length; i++) {
41+
// Swap currentMax and currentMin if current number is negative
3942
if (nums[i] < 0) {
4043
int temp = currentMax;
4144
currentMax = currentMin;
4245
currentMin = temp;
4346
}
4447

48+
// Update currentMax and currentMin
4549
currentMax = Math.max(nums[i], currentMax * nums[i]);
4650
currentMin = Math.min(nums[i], currentMin * nums[i]);
4751

52+
// Update global max product
4853
maxProduct = Math.max(maxProduct, currentMax);
4954
}
5055

5156
return maxProduct;
5257
}
5358
}
54-
55-
/**
56-
* A recursive helper method to calculate the product of elements from index
57-
* start to index end using memoization.
58-
*
59-
* @param nums the input array of integers.
60-
* @param memo the memoization table storing the results of subproblems.
61-
* @param start the starting index of the subarray.
62-
* @param end the ending index of the subarray.
63-
* @return the product of elements from start to end.
64-
*/
65-
private static int calculateProduct(int[] nums, Integer[][] memo, int start, int end) {
66-
if (memo[start][end] != null) {
67-
return memo[start][end];
68-
}
69-
70-
if (start == end) {
71-
memo[start][end] = nums[start];
72-
return nums[start];
73-
}
74-
75-
int product = calculateProduct(nums, memo, start, end - 1) * nums[end];
76-
memo[start][end] = product;
77-
return product;
78-
}
79-
}

0 commit comments

Comments
 (0)