-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum-product-subarray.js
26 lines (22 loc) · 1 KB
/
maximum-product-subarray.js
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
/**
* @param {number[]} nums
* @return {number}
*/
var maxProduct = function(nums) {
// Max product in the current contiguous array
let currentMax = nums[0];
// Mind product in the current contiguous array. We need this value in case we encounter 2 negative numbers whose product could potentially give us the max product of the entire array
let currentMin = nums[0];
// Max product of a contiguous array
let finalMax = nums[0];
for(let i = 1; i < nums.length; i++){
let temp = currentMax
// Because we are looking for a contiguous subarray product, the current max must contain the current number in the array.
currentMax = Math.max(Math.max(currentMax * nums[i], currentMin*nums[i]), nums[i])
// Use temp here in case the previous currentMax was negative
currentMin = Math.min(Math.min(temp * nums[i], currentMin*nums[i]), nums[i])
// Record highest max at the end of every contiguous subarray
finalMax = Math.max(currentMax, finalMax);
}
return finalMax;
};