-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy path152-Maximum-Product-Subarray.js
More file actions
40 lines (36 loc) · 947 Bytes
/
152-Maximum-Product-Subarray.js
File metadata and controls
40 lines (36 loc) · 947 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* https://leetcode.com/problems/maximum-product-subarray/description/
* Difficulty:Medium
*
* Find the contiguous subarray within an array (containing at least one number)
* which has the largest product.
*
* For example, given the array [2,3,-2,4],
* the contiguous subarray [2,3] has the largest product = 6.
*/
/**
* @param {number[]} nums
* @return {number}
*/
var maxProduct = function (nums) {
var a = nums[0];
var imin = a;
var imax = a;
var max = a;
for (var i = 1; i < nums.length; i++) {
var t = nums[i];
if (t < 0) {
var tmp = imin;
imin = imax;
imax = tmp;
}
imax = Math.max(t, t * imax);
imin = Math.min(t, t * imin);
max = Math.max(max, imax);
}
return max;
};
console.log(maxProduct([-1]));
console.log(maxProduct([1]));
console.log(maxProduct([1, 2, 3, -4]));
console.log(maxProduct([2, 3, -2, 4]));