Skip to content

Commit

Permalink
Solved LeetCode Maximum Product SubArray
Browse files Browse the repository at this point in the history
  • Loading branch information
ghsatpute committed Apr 18, 2023
1 parent 025843e commit e44b39b
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package problemsolving.leetcode.algorithm.dynamicProgramming.maximumProductSubarray;

public class MaximumProductSubArray {
public int maxProduct(int[] nums) {
// We want to keep track of current Min because there might be subarray [-2, 2, 2]
// So far current max would be 4, and current min would be -8
// Now, if current number is [-1], the new max would become -1 * 8 i.e. 8 which is greater
// than current max
int curMin = nums[0];
int curMax = nums[0];
int output = nums[0];
for (int i = 1; i < nums.length; i++) {
int temp = curMin;
curMin = Math.min(
// current number itself could be lesser than current min
nums[i],
Math.min(
// Check if we get less number by multiplying current number with current max
// In case, current number is negative we could get bigger min
curMax * nums[i],
// Check if we get less number than curr min by multiplying current number with current min
curMin * nums[i]
));

curMax = Math.max(
// Current number itself could be max number so far
nums[i],
Math.max(
// Check if we get bigger number than current max by multiplyign current number with current max
curMax * nums[i],
// Check if we get bigger number than current max by multiplying current number with current min
// In case, current number is negative we can get bigger number
temp * nums[i]
));

if (curMax > output) {
output = curMax;
}
}

return output;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 152. Maximum Product Subarray

Given an integer array `nums`, find a subarray
that has the largest product, and return the product.

The test cases are generated so that the answer will fit in a 32-bit integer.

### Example 1
```
Input: nums = [2,3,-2,4]
Output: 6
Explanation: [2,3] has the largest product 6.
```

### Example 2
```
Input: nums = [-2,0,-1]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
```

## Constraints
* `1 <= nums.length <= 2 * 10^4`
* `-10 <= nums[i] <= 10`
* The product of any prefix or suffix of `nums` is guaranteed to fit in a 32-bit integer.

# Solution Reference
https://www.youtube.com/watch?v=lXVy6YWFcRM&ab_channel=NeetCode
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package problemsolving.leetcode.algorithm.dynamicProgramming.maximumProductSubarray;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

class MaximumProductSubArrayTest {
@Test
public void testCase01() {
int[] input = {2, 3, -2, 4};

int output = new MaximumProductSubArray().maxProduct(input);

assertEquals(6, output);
}

@Test
public void testCase02() {
int[] input = {-2,0,-1};

int output = new MaximumProductSubArray().maxProduct(input);

assertEquals(0, output);
}

@Test
public void testCase03() {
int[] input = {-3,-1,-1};

int output = new MaximumProductSubArray().maxProduct(input);

assertEquals(3 , output);
}
}

0 comments on commit e44b39b

Please sign in to comment.