Skip to content

Commit 9980cf4

Browse files
committed
Add solution #152
1 parent bc1b125 commit 9980cf4

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
66|[Plus One](./0066-plus-one.js)|Easy|
1414
67|[Add Binary](./0067-add-binary.js)|Easy|
1515
151|[Reverse Words in a String](./0151-reverse-words-in-a-string.js)|Medium|
16+
152|[Maximum Product Subarray](./0152-maximum-product-subarray.js)|Medium|
1617
226|[Invert Binary Tree](./0226-invert-binary-tree.js)|Easy|
1718
263|[Ugly Number](./0263-ugly-number.js)|Easy|
1819
264|[Ugly Number II](./0264-ugly-number-ii.js)|Medium|
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* 152. Maximum Product Subarray
3+
* https://leetcode.com/problems/maximum-product-subarray/
4+
* Difficulty: Medium
5+
*
6+
* Given an integer array nums, find the contiguous subarray within an array
7+
* (containing at least one number) which has the largest product.
8+
*/
9+
10+
/**
11+
* @param {number[]} nums
12+
* @return {number}
13+
*/
14+
var maxProduct = function(nums) {
15+
let result = nums[0];
16+
let min = 1;
17+
let max = 1;
18+
for (let n of nums) {
19+
[min, max] = [Math.min(n, min * n, max * n), Math.max(n, min * n, max * n)];
20+
result = Math.max(result, max);
21+
}
22+
return result;
23+
};

0 commit comments

Comments
 (0)