Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0f56698

Browse files
authoredSep 24, 2020
Update 152-maximum-product-subarray.js
1 parent 1d6e6ec commit 0f56698

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
 

‎152-maximum-product-subarray.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,22 @@ const maxProduct = function(nums) {
4545
}
4646
return max;
4747
};
48+
49+
// another
50+
51+
/**
52+
* @param {number[]} nums
53+
* @return {number}
54+
*/
55+
const maxProduct = function(nums) {
56+
const n = nums.length
57+
let max, min
58+
let res = max = min = nums[0]
59+
for(let i = 1; i < n; i++) {
60+
if(nums[i] < 0) [max, min] = [min, max]
61+
max = Math.max(nums[i], nums[i] * max)
62+
min = Math.min(nums[i], nums[i] * min)
63+
res = Math.max(res, max)
64+
}
65+
return res
66+
};

0 commit comments

Comments
 (0)
Please sign in to comment.