-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax-product-subarray.js
More file actions
42 lines (34 loc) · 894 Bytes
/
max-product-subarray.js
File metadata and controls
42 lines (34 loc) · 894 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
41
42
var log = console.log;
/**
* @description https://leetcode.com/problems/maximum-product-subarray/ #152
* @param {number[]} nums
* @return {number}
*/
var maxProduct = function(nums) {
var numLen = nums.length;
if (numLen == 1) return nums[0];
if (numLen == 0) return 0;
var lastNumber = 1;
var begin = 0;
var end = 0;
var currProduct = -Infinity;
var ans = currProduct;
while (begin < numLen) {
firstNumber = nums[begin];
end = begin;
while (end < numLen) {
lastNumber = nums[end];
if (end == begin) {
currProduct = nums[begin];
} else {
currProduct *= lastNumber;
}
counter++;
ans = Math.max(ans, currProduct);
end++;
}
begin++;
}
return ans;
};
console.log(maxProduct([-2,3,-4]));