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 5a02af8

Browse files
authoredApr 27, 2019
Create 152-maximum-product-subarray.js
1 parent d6d89f7 commit 5a02af8

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
 

‎152-maximum-product-subarray.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const maxProduct = function(nums) {
6+
if(nums.length == 1)return nums[0];
7+
let dpMax = nums[0];
8+
let dpMin = nums[0];
9+
let max = nums[0];
10+
for (let i = 1; i < nums.length; i++) {
11+
let k = dpMax*nums[i];
12+
let m = dpMin*nums[i];
13+
dpMax = Math.max(nums[i], Math.max(k, m));
14+
dpMin = Math.min(nums[i], Math.min(k, m));
15+
max = Math.max(dpMax, max);
16+
}
17+
return max;
18+
};

0 commit comments

Comments
 (0)
Please sign in to comment.