-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solved LeetCode Maximum Product SubArray
- Loading branch information
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
.../leetcode/algorithm/dynamicProgramming/maximumProductSubarray/MaximumProductSubArray.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...msolving/leetcode/algorithm/dynamicProgramming/maximumProductSubarray/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
34 changes: 34 additions & 0 deletions
34
...tcode/algorithm/dynamicProgramming/maximumProductSubarray/MaximumProductSubArrayTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |