Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9fc54fe

Browse files
Add solution to 152.
1 parent fec4930 commit 9fc54fe

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var maxProduct = function (nums) {
6+
if (nums.length === 0) {
7+
return 0;
8+
}
9+
let result = nums[0];
10+
11+
let min_so_far = nums[0];
12+
let max_so_far = nums[0];
13+
14+
for (let i = 1; i < nums.length; i++) {
15+
let current = nums[i];
16+
temp_max = Math.max(current, current * max_so_far, current * min_so_far);
17+
min_so_far = Math.min(current, current * max_so_far, current * min_so_far);
18+
19+
max_so_far = temp_max;
20+
21+
result = Math.max(result, max_so_far);
22+
}
23+
24+
return result;
25+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /